SDK error revamp (#432)
SDK methods now return 1 error. To differentiate between Client errors and Server errors I've also implemented the AsServerError function which returns an *api.Error if it is an error originating from the remote server or nil otherwise.
This commit is contained in:
1 parent
94f9d952be
commit
fd0da998e8
53 files changed
+1512
-1635
No files matched your search
@@ -4,13 +4,12 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/api/authtokens"
|
||||
)
|
||||
|
||||
func (c *Client) Authenticate(ctx context.Context, authMethodId string, credentials map[string]interface{}, opt ...Option) (*authtokens.AuthTokenReadResult, *api.Error, error) {
|
||||
func (c *Client) Authenticate(ctx context.Context, authMethodId string, credentials map[string]interface{}, opt ...Option) (*authtokens.AuthTokenReadResult, error) {
|
||||
if c.client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in Authenticate request")
|
||||
return nil, fmt.Errorf("nil client in Authenticate request")
|
||||
}
|
||||
|
||||
_, apiOpts := getOpts(opt...)
|
||||
@@ -21,20 +20,23 @@ func (c *Client) Authenticate(ctx context.Context, authMethodId string, credenti
|
||||
|
||||
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("auth-methods/%s:authenticate", authMethodId), reqBody, apiOpts...)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating Authenticate request: %w", err)
|
||||
return nil, fmt.Errorf("error creating Authenticate request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during Authenticate call: %w", err)
|
||||
return nil, fmt.Errorf("error performing client request during Authenticate call: %w", err)
|
||||
}
|
||||
|
||||
target := new(authtokens.AuthTokenReadResult)
|
||||
target.Item = new(authtokens.AuthToken)
|
||||
apiErr, err := resp.Decode(target.Item)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding Authenticate response: %w", err)
|
||||
return nil, fmt.Errorf("error decoding Authenticate response: %w", err)
|
||||
}
|
||||
if apiErr != nil {
|
||||
return nil, apiErr
|
||||
}
|
||||
|
||||
return target, apiErr, nil
|
||||
return target, nil
|
||||
}
|
||||
Reference in new issue
Block a user