Skip to content

Commit f2cf939

Browse files
authored
Return APIErrors directly instead of as a stringified error (#22)
* Return APIErrors directly instead of as a stringified error * Back out "Return APIErrors directly instead of as a stringified error" This backs out commit 37676ce. * Return a custom error if we get an invalid enrollment code error * Alter the naming pattern for the errors
1 parent fc9d658 commit f2cf939

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

client.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,8 @@ func (e *APIError) Unwrap() error {
7979
return e.e
8080
}
8181

82-
type InvalidCredentialsError struct{}
83-
84-
func (e InvalidCredentialsError) Error() string {
85-
return "invalid credentials"
86-
}
82+
var ErrInvalidCredentials = fmt.Errorf("invalid credentials")
83+
var ErrInvalidCode = fmt.Errorf("invalid enrollment code")
8784

8885
type EnrollMeta struct {
8986
OrganizationID string
@@ -163,6 +160,13 @@ func (c *Client) Enroll(ctx context.Context, logger logrus.FieldLogger, code str
163160
return nil, nil, nil, nil, &APIError{e: fmt.Errorf("error decoding JSON response: %s\nbody: %s", err, b), ReqID: reqID}
164161
}
165162

163+
// Check for *only* an "invalid code" error returned by the API
164+
if len(r.Errors) == 1 {
165+
if err := r.Errors[0]; err.Path == "code" && err.Code == "ERR_INVALID_VALUE" {
166+
return nil, nil, nil, nil, &APIError{e: ErrInvalidCode, ReqID: reqID}
167+
}
168+
}
169+
166170
// Check for any errors returned by the API
167171
if err := r.Errors.ToError(); err != nil {
168172
return nil, nil, nil, nil, &APIError{e: fmt.Errorf("unexpected error during enrollment: %v", err), ReqID: reqID}
@@ -404,7 +408,7 @@ func (c *Client) streamingPostDNClient(ctx context.Context, reqType string, valu
404408
case http.StatusOK:
405409
sc.respBytes = respBody
406410
case http.StatusUnauthorized:
407-
sc.err.Store(InvalidCredentialsError{})
411+
sc.err.Store(ErrInvalidCredentials)
408412
default:
409413
var errors struct {
410414
Errors message.APIErrors
@@ -451,7 +455,7 @@ func (c *Client) postDNClient(ctx context.Context, reqType string, value []byte,
451455
case http.StatusOK:
452456
return respBody, nil
453457
case http.StatusUnauthorized:
454-
return nil, InvalidCredentialsError{}
458+
return nil, ErrInvalidCredentials
455459
default:
456460
var errors struct {
457461
Errors message.APIErrors

client_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,7 @@ func TestDoUpdate(t *testing.T) {
237237
defer cancel()
238238
_, err = c.CheckForUpdate(ctx, invalidCreds)
239239
assert.Error(t, err)
240-
invalidCredsErrorType := InvalidCredentialsError{}
241-
assert.ErrorAs(t, err, &invalidCredsErrorType)
240+
assert.ErrorIs(t, err, ErrInvalidCredentials)
242241
serverErrs := ts.Errors() // This consumes/resets the server errors
243242
require.Len(t, serverErrs, 1)
244243

@@ -427,8 +426,7 @@ func TestDoUpdate_P256(t *testing.T) {
427426
defer cancel()
428427
_, err = c.CheckForUpdate(ctx, invalidCreds)
429428
assert.Error(t, err)
430-
invalidCredsErrorType := InvalidCredentialsError{}
431-
assert.ErrorAs(t, err, &invalidCredsErrorType)
429+
assert.ErrorIs(t, err, ErrInvalidCredentials)
432430
serverErrs := ts.Errors() // This consumes/resets the server errors
433431
require.Len(t, serverErrs, 1)
434432

0 commit comments

Comments
 (0)