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

+22 -32
View File
@@ -5,6 +5,7 @@ import (
"sort"
"testing"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/accounts"
"github.com/hashicorp/boundary/api/authmethods"
"github.com/hashicorp/boundary/api/authtokens"
@@ -32,61 +33,51 @@ func TestList(t *testing.T) {
org := iam.TestOrg(t, tc.IamRepo(), iam.WithUserId(token.UserId))
amClient := authmethods.NewClient(client)
amResult, apiErr, err := amClient.Create(tc.Context(), "password", org.GetPublicId())
amResult, err := amClient.Create(tc.Context(), "password", org.GetPublicId())
require.NoError(err)
require.Nil(apiErr)
require.NotNil(amResult)
amId = amResult.Item.Id
rolesClient := roles.NewClient(client)
role, apiErr, err := rolesClient.Create(tc.Context(), org.GetPublicId())
role, err := rolesClient.Create(tc.Context(), org.GetPublicId())
require.NoError(err)
require.Nil(apiErr)
require.NotNil(role)
role, apiErr, err = rolesClient.AddPrincipals(tc.Context(), role.Item.Id, 0, []string{"u_anon"}, roles.WithAutomaticVersioning(true))
role, err = rolesClient.AddPrincipals(tc.Context(), role.Item.Id, 0, []string{"u_anon"}, roles.WithAutomaticVersioning(true))
require.NoError(err)
require.Nil(apiErr)
require.NotNil(role)
role, apiErr, err = rolesClient.AddGrants(tc.Context(), role.Item.Id, 0, []string{"type=auth-method;actions=authenticate"}, roles.WithAutomaticVersioning(true))
role, err = rolesClient.AddGrants(tc.Context(), role.Item.Id, 0, []string{"type=auth-method;actions=authenticate"}, roles.WithAutomaticVersioning(true))
require.NoError(err)
require.Nil(apiErr)
require.NotNil(role)
acctClient := accounts.NewClient(client)
acct, apiErr, err := acctClient.Create(tc.Context(), amId, accounts.WithPasswordAccountLoginName("user"), accounts.WithPasswordAccountPassword("passpass"))
acct, err := acctClient.Create(tc.Context(), amId, accounts.WithPasswordAccountLoginName("user"), accounts.WithPasswordAccountPassword("passpass"))
require.NoError(err)
require.Nil(apiErr)
require.NotNil(acct)
tokens := authtokens.NewClient(client)
atl, apiErr, err := tokens.List(tc.Context(), org.GetPublicId())
atl, err := tokens.List(tc.Context(), org.GetPublicId())
require.NoError(err)
assert.Nil(apiErr)
assert.Empty(atl.Items)
var expected []*authtokens.AuthToken
methods := authmethods.NewClient(client)
at, apiErr, err := methods.Authenticate(tc.Context(), amId, map[string]interface{}{"login_name": "user", "password": "passpass"})
at, err := methods.Authenticate(tc.Context(), amId, map[string]interface{}{"login_name": "user", "password": "passpass"})
require.NoError(err)
assert.Nil(apiErr)
expected = append(expected, at.Item)
atl, apiErr, err = tokens.List(tc.Context(), org.GetPublicId())
atl, err = tokens.List(tc.Context(), org.GetPublicId())
require.NoError(err)
assert.Nil(apiErr)
assert.ElementsMatch(comparableSlice(expected), comparableSlice(atl.Items))
for i := 1; i < 10; i++ {
at, apiErr, err = methods.Authenticate(tc.Context(), amId, map[string]interface{}{"login_name": "user", "password": "passpass"})
at, err = methods.Authenticate(tc.Context(), amId, map[string]interface{}{"login_name": "user", "password": "passpass"})
require.NoError(err)
assert.Nil(apiErr)
expected = append(expected, at.Item)
}
atl, apiErr, err = tokens.List(tc.Context(), org.GetPublicId())
atl, err = tokens.List(tc.Context(), org.GetPublicId())
require.NoError(err)
assert.Nil(apiErr)
assert.ElementsMatch(comparableSlice(expected), comparableSlice(atl.Items))
}
@@ -129,18 +120,15 @@ func TestCrud(t *testing.T) {
tokens := authtokens.NewClient(client)
methods := authmethods.NewClient(client)
want, apiErr, err := methods.Authenticate(tc.Context(), amId, map[string]interface{}{"login_name": "user", "password": "passpass"})
want, err := methods.Authenticate(tc.Context(), amId, map[string]interface{}{"login_name": "user", "password": "passpass"})
require.NoError(err)
assert.Empty(apiErr)
at, apiErr, err := tokens.Read(tc.Context(), want.Item.Id)
at, err := tokens.Read(tc.Context(), want.Item.Id)
require.NoError(err)
assert.Nil(apiErr)
assert.EqualValues(comparableResource(want.Item), comparableResource(at.Item))
_, _, err = tokens.Delete(tc.Context(), at.Item.Id)
_, err = tokens.Delete(tc.Context(), at.Item.Id)
require.NoError(err)
assert.Nil(apiErr)
}
func TestErrors(t *testing.T) {
@@ -153,13 +141,15 @@ func TestErrors(t *testing.T) {
client.SetToken(token.Token)
tokens := authtokens.NewClient(client)
_, apiErr, err := tokens.Read(tc.Context(), authtoken.AuthTokenPrefix+"_doesntexis")
require.NoError(err)
assert.NotNil(apiErr)
_, err := tokens.Read(tc.Context(), authtoken.AuthTokenPrefix+"_doesntexis")
require.Error(err)
apiErr := api.AsServerError(err)
require.NotNil(apiErr)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
_, apiErr, err = tokens.Read(tc.Context(), "invalid id")
require.NoError(err)
assert.NotNil(apiErr)
_, err = tokens.Read(tc.Context(), "invalid id")
require.Error(err)
apiErr = api.AsServerError(err)
require.NotNil(apiErr)
assert.EqualValues(http.StatusBadRequest, apiErr.Status)
}
+24 -24
View File
@@ -107,19 +107,19 @@ func (c *Client) ApiClient() *api.Client {
return c.client
}
func (c *Client) Read(ctx context.Context, authTokenId string, opt ...Option) (*AuthTokenReadResult, *api.Error, error) {
func (c *Client) Read(ctx context.Context, authTokenId string, opt ...Option) (*AuthTokenReadResult, error) {
if authTokenId == "" {
return nil, nil, fmt.Errorf("empty authTokenId value passed into Read request")
return nil, fmt.Errorf("empty authTokenId value passed into Read request")
}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
return nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("auth-tokens/%s", authTokenId), nil, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Read request: %w", err)
return nil, fmt.Errorf("error creating Read request: %w", err)
}
if len(opts.queryMap) > 0 {
@@ -132,36 +132,36 @@ func (c *Client) Read(ctx context.Context, authTokenId string, opt ...Option) (*
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Read call: %w", err)
return nil, fmt.Errorf("error performing client request during Read call: %w", err)
}
target := new(AuthTokenReadResult)
target.Item = new(AuthToken)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Read response: %w", err)
return nil, fmt.Errorf("error decoding Read response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
return nil, apiErr
}
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
return target, nil
}
func (c *Client) Delete(ctx context.Context, authTokenId string, opt ...Option) (*AuthTokenDeleteResult, *api.Error, error) {
func (c *Client) Delete(ctx context.Context, authTokenId string, opt ...Option) (*AuthTokenDeleteResult, error) {
if authTokenId == "" {
return nil, nil, fmt.Errorf("empty authTokenId value passed into Delete request")
return nil, fmt.Errorf("empty authTokenId value passed into Delete request")
}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
return nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("auth-tokens/%s", authTokenId), nil, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating Delete request: %w", err)
return nil, fmt.Errorf("error creating Delete request: %w", err)
}
if len(opts.queryMap) > 0 {
@@ -174,30 +174,30 @@ func (c *Client) Delete(ctx context.Context, authTokenId string, opt ...Option)
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during Delete call: %w", err)
return nil, fmt.Errorf("error performing client request during Delete call: %w", err)
}
apiErr, err := resp.Decode(nil)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Delete response: %w", err)
return nil, fmt.Errorf("error decoding Delete response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
return nil, apiErr
}
target := &AuthTokenDeleteResult{
responseBody: resp.Body,
responseMap: resp.Map,
}
return target, nil, nil
return target, nil
}
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) (*AuthTokenListResult, *api.Error, error) {
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) (*AuthTokenListResult, error) {
if scopeId == "" {
return nil, nil, fmt.Errorf("empty scopeId value passed into List request")
return nil, fmt.Errorf("empty scopeId value passed into List request")
}
if c.client == nil {
return nil, nil, fmt.Errorf("nil client")
return nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
@@ -205,7 +205,7 @@ func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) (*Auth
req, err := c.client.NewRequest(ctx, "GET", "auth-tokens", nil, apiOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating List request: %w", err)
return nil, fmt.Errorf("error creating List request: %w", err)
}
if len(opts.queryMap) > 0 {
@@ -218,18 +218,18 @@ func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) (*Auth
resp, err := c.client.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("error performing client request during List call: %w", err)
return nil, fmt.Errorf("error performing client request during List call: %w", err)
}
target := new(AuthTokenListResult)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding List response: %w", err)
return nil, fmt.Errorf("error decoding List response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
return nil, apiErr
}
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
return target, nil
}