Skip to content

Commit

Permalink
Merge pull request #642 from hashicorp/f--aws-sdk-go-v2-ErrStatusCode…
Browse files Browse the repository at this point in the history
…Equals

Add `tfawserr.ErrHTTPStatusCodeEquals`, AWS SDK for Go v2 variant of `v2/awsv1shim/tfawserr.ErrStatusCodeEquals`
  • Loading branch information
ewbankkit authored Sep 5, 2023
2 parents ab8186f + 2572ed0 commit 95ea2bd
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tfawserr/awserr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

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

Expand Down Expand Up @@ -34,3 +35,17 @@ func ErrMessageContains(err error, code string, message string) bool {
}
return false
}

// ErrHTTPStatusCodeEquals returns true if the error matches all these conditions:
// - err is of type smithyhttp.ResponseError
// - ResponseError.HTTPStatusCode() equals one of the passed status codes
func ErrHTTPStatusCodeEquals(err error, statusCodes ...int) bool {
if respErr, ok := errs.As[*smithyhttp.ResponseError](err); ok {
for _, statusCode := range statusCodes {
if respErr.HTTPStatusCode() == statusCode {
return true
}
}
}
return false
}
62 changes: 62 additions & 0 deletions tfawserr/awserr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ package tfawserr

import (
"fmt"
"net/http"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sts/types"
smithy "github.com/aws/smithy-go"
smithyhttp "github.com/aws/smithy-go/transport/http"
)

func TestErrCodeEquals(t *testing.T) {
Expand Down Expand Up @@ -224,3 +226,63 @@ func TestErrMessageContains(t *testing.T) {
})
}
}

func TestErrHTTPStatusCodeEquals(t *testing.T) {
testCases := map[string]struct {
Err error
Codes []int
Expected bool
}{
"nil error": {
Err: nil,
Expected: false,
},
"other error": {
Err: fmt.Errorf("other error"),
Expected: false,
},
"Top-level smithyhttp.ResponseError matching first code": {
Err: &smithyhttp.ResponseError{Response: &smithyhttp.Response{Response: &http.Response{StatusCode: http.StatusNotFound}}},
Codes: []int{http.StatusNotFound},
Expected: true,
},
"Top-level smithyhttp.ResponseError matching last code": {
Err: &smithyhttp.ResponseError{Response: &smithyhttp.Response{Response: &http.Response{StatusCode: http.StatusOK}}},
Codes: []int{http.StatusNotFound, http.StatusOK},
Expected: true,
},
"Top-level smithyhttp.ResponseError no code": {
Err: &smithyhttp.ResponseError{Response: &smithyhttp.Response{Response: &http.Response{StatusCode: http.StatusOK}}},
},
"Top-level smithyhttp.ResponseError non-matching codes": {
Err: &smithyhttp.ResponseError{Response: &smithyhttp.Response{Response: &http.Response{StatusCode: http.StatusOK}}},
Codes: []int{http.StatusNotFound, http.StatusNoContent},
},
"Wrapped smithyhttp.ResponseError matching first code": {
Err: &smithy.OperationError{Err: &smithyhttp.ResponseError{Response: &smithyhttp.Response{Response: &http.Response{StatusCode: http.StatusNotFound}}}},
Codes: []int{http.StatusNotFound},
Expected: true,
},
"Wrapped smithyhttp.ResponseError matching last code": {
Err: &smithy.OperationError{Err: &smithyhttp.ResponseError{Response: &smithyhttp.Response{Response: &http.Response{StatusCode: http.StatusOK}}}},
Codes: []int{http.StatusNotFound, http.StatusOK},
Expected: true,
},
"Wrapped smithyhttp.ResponseError non-matching codes": {
Err: &smithy.OperationError{Err: &smithyhttp.ResponseError{Response: &smithyhttp.Response{Response: &http.Response{StatusCode: http.StatusOK}}}},
Codes: []int{http.StatusNotFound, http.StatusNoContent},
},
}

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

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

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

0 comments on commit 95ea2bd

Please sign in to comment.