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:
Todd Knight authored and GitHub committed 2020-09-24 14:34:58 -07:00
1 parent 94f9d952be
commit fd0da998e8
53 files changed
+1512 -1635

No files matched your search

+8 -10
View File
@@ -5,8 +5,6 @@ import (
"context"
"fmt"
"net/url"
"github.com/hashicorp/boundary/api"
)
type SessionAuthorizationResult struct {
@@ -27,20 +25,20 @@ func (n SessionAuthorizationResult) GetResponseMap() map[string]interface{} {
return n.responseMap
}
func (c *Client) Authorize(ctx context.Context, targetId string, opt ...Option) (*SessionAuthorizationResult, *api.Error, error) {
func (c *Client) Authorize(ctx context.Context, targetId string, opt ...Option) (*SessionAuthorizationResult, error) {
if targetId == "" {
return nil, nil, fmt.Errorf("empty targetId value passed into Authorize request")
return nil, fmt.Errorf("empty targetId value passed into Authorize request")
}
opts, apiOpts := getOpts(opt...)
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
return nil, fmt.Errorf("nil client")
}
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("targets/%s:authorize", targetId), opts.postMap, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Authorize request: %w", err)
return nil, fmt.Errorf("error creating Authorize request: %w", err)
}
if len(opts.queryMap) > 0 {
@@ -53,19 +51,19 @@ func (c *Client) Authorize(ctx context.Context, targetId string, opt ...Option)
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Authorize call: %w", err)
return nil, fmt.Errorf("error performing client request during Authorize call: %w", err)
}
sar := new(SessionAuthorizationResult)
sar.Item = new(SessionAuthorization)
apiErr, err := resp.Decode(sar.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Authorize response: %w", err)
return nil, fmt.Errorf("error decoding Authorize response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
return nil, apiErr
}
sar.responseBody = resp.Body
sar.responseMap = resp.Map
return sar, apiErr, nil
return sar, nil
}