Skip to content

Commit

Permalink
Add 'tfawserr.ErrMessageContains'.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Jun 29, 2023
1 parent 2534cec commit 7c7377e
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tfawserr/awserr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
package tfawserr

import (
"strings"

smithy "github.com/aws/smithy-go"
"github.com/hashicorp/aws-sdk-go-base/v2/internal/errs"
)

// ErrCodeEquals returns true if the error matches all these conditions:
// - err is of type smithy.APIError
// - Error.Code() equals one of the passed codes
// - APIError.ErrorCode() equals one of the passed codes
func ErrCodeEquals(err error, codes ...string) bool {
if apiErr, ok := errs.As[smithy.APIError](err); ok {
for _, code := range codes {
Expand All @@ -21,3 +23,14 @@ func ErrCodeEquals(err error, codes ...string) bool {
}
return false
}

// ErrMessageContains returns true if the error matches all these conditions:
// - err is of type smithy.APIError
// - APIError.ErrorCode() equals code
// - APIError.ErrorMessage() contains message
func ErrMessageContains(err error, code string, message string) bool {
if apiErr, ok := errs.As[smithy.APIError](err); ok {
return apiErr.ErrorCode() == code && strings.Contains(apiErr.ErrorMessage(), message)
}
return false
}
133 changes: 133 additions & 0 deletions tfawserr/awserr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,136 @@ func TestErrCodeEquals(t *testing.T) {
})
}
}

func TestErrMessageContains(t *testing.T) {
testCases := map[string]struct {
Err error
Code string
Message string
Expected bool
}{
"nil error": {
Err: nil,
Expected: false,
},
"nil error code": {
Err: nil,
Code: "test",
Expected: false,
},
"nil error message": {
Err: nil,
Message: "test",
},
"nil error code and message": {
Err: nil,
Code: "test",
Message: "test",
},
"other error": {
Err: fmt.Errorf("other error"),
Expected: false,
},
"other error code and message": {
Err: fmt.Errorf("other error"),
Code: "test",
Message: "test",
},
"Top-level smithy.GenericAPIError no code": {
Err: &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"},
},
"Top-level smithy.GenericAPIError matching code and no message": {
Err: &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"},
Code: "TestCode",
Expected: true,
},
"Top-level smithy.GenericAPIError matching code and matching message exact": {
Err: &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"},
Code: "TestCode",
Message: "TestMessage",
Expected: true,
},
"Top-level smithy.GenericAPIError non-matching code and matching message exact": {
Err: &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"},
Code: "NotMatching",
Message: "TestMessage",
},
"Top-level smithy.GenericAPIError matching code and matching message contains": {
Err: &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"},
Code: "TestCode",
Message: "estMess",
Expected: true,
},
"Top-level smithy.GenericAPIError matching code and non-matching message": {
Err: &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"},
Code: "TestCode",
Message: "NotMatching",
},
"Wrapped smithy.GenericAPIError matching code and no message": {
Err: fmt.Errorf("test: %w", &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"}),
Code: "TestCode",
Expected: true,
},
"Wrapped smithy.GenericAPIError matching code and matching message exact": {
Err: fmt.Errorf("test: %w", &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"}),
Code: "TestCode",
Message: "TestMessage",
Expected: true,
},
"Wrapped smithy.GenericAPIError non-matching code and matching message exact": {
Err: fmt.Errorf("test: %w", &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"}),
Code: "NotMatching",
Message: "TestMessage",
},
"Wrapped smithy.GenericAPIError matching code and matching message contains": {
Err: fmt.Errorf("test: %w", &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"}),
Code: "TestCode",
Message: "estMess",
Expected: true,
},
"Wrapped smithy.GenericAPIError matching code and non-matching message": {
Err: fmt.Errorf("test: %w", &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"}),
Code: "TestCode",
Message: "NotMatching",
},
"Top-level sts ExpiredTokenException matching code and no message": {
Err: &types.ExpiredTokenException{ErrorCodeOverride: aws.String("TestCode"), Message: aws.String("TestMessage")},
Code: "TestCode",
Expected: true,
},
"Top-level sts ExpiredTokenException matching code and matching message exact": {
Err: &types.ExpiredTokenException{ErrorCodeOverride: aws.String("TestCode"), Message: aws.String("TestMessage")},
Code: "TestCode",
Message: "TestMessage",
Expected: true,
},
"Top-level sts ExpiredTokenException non-matching code and matching message exact": {
Err: &types.ExpiredTokenException{ErrorCodeOverride: aws.String("TestCode"), Message: aws.String("TestMessage")},
Code: "NotMatching",
Message: "TestMessage",
},
"Top-level sts ExpiredTokenException matching code and matching message contains": {
Err: &types.ExpiredTokenException{ErrorCodeOverride: aws.String("TestCode"), Message: aws.String("TestMessage")},
Code: "TestCode",
Message: "estMess",
Expected: true,
},
"Top-level sts ExpiredTokenException matching code and non-matching message": {
Err: &types.ExpiredTokenException{ErrorCodeOverride: aws.String("TestCode"), Message: aws.String("TestMessage")},
Code: "TestCode",
Message: "NotMatching",
},
}

for name, testCase := range testCases {
testCase := testCase

t.Run(name, func(t *testing.T) {
got := ErrMessageContains(testCase.Err, testCase.Code, testCase.Message)

if got != testCase.Expected {
t.Errorf("got %t, expected %t", got, testCase.Expected)
}
})
}
}

0 comments on commit 7c7377e

Please sign in to comment.