Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tfawserr.ErrCodeContains, AWS SDK for Go v2 variant of v2/awsv1shim/tfawserr.ErrCodeContains #733

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tfawserr/awserr.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ func ErrCodeEquals(err error, codes ...string) bool {
return false
}

// ErrCodeContains returns true if the error matches all these conditions:
// - err is of type smithy.APIError
// - APIError.ErrorCode() contains code
func ErrCodeContains(err error, code string) bool {
if apiErr, ok := errs.As[smithy.APIError](err); ok {
return strings.Contains(apiErr.ErrorCode(), code)
}
return false
}

// ErrMessageContains returns true if the error matches all these conditions:
// - err is of type smithy.APIError
// - APIError.ErrorCode() equals code
Expand Down
52 changes: 52 additions & 0 deletions tfawserr/awserr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,58 @@ func TestErrCodeEquals(t *testing.T) {
}
}

func TestErrCodeContains(t *testing.T) {
testCases := map[string]struct {
Err error
Code string
Expected bool
}{
"nil error": {
Err: nil,
Expected: false,
},
"other error": {
Err: fmt.Errorf("other error"),
Expected: false,
},
"Top-level smithy.GenericAPIError contains": {
Err: &smithy.GenericAPIError{Code: "TestCoder", Message: "TestMessage"},
Code: "TestCode",
Expected: true,
},
"Top-level smithy.GenericAPIError does not contain": {
Err: &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"},
Code: "NotMatching",
},
"Wrapped smithy.GenericAPIError contains": {
Err: fmt.Errorf("test: %w", &smithy.GenericAPIError{Code: "ATestCode", Message: "TestMessage"}),
Code: "TestCode",
Expected: true,
},
"Wrapped smithy.GenericAPIError does not contain": {
Err: fmt.Errorf("test: %w", &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"}),
Code: "AlsoNotMatching",
},
"Top-level sts ExpiredTokenException contains": {
Err: &types.ExpiredTokenException{ErrorCodeOverride: aws.String("ATestCoder"), Message: aws.String("TestMessage")},
Code: "TestCode",
Expected: true,
},
}

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

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

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

func TestErrMessageContains(t *testing.T) {
testCases := map[string]struct {
Err error
Expand Down