Skip to content
New issue

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

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

Already on GitHub? # to your account

Use request body error message for unknown errors missing a message header #4740

Merged
merged 3 commits into from
Mar 15, 2023
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
@@ -3,3 +3,5 @@
### SDK Enhancements

### SDK Bugs

- `private/protocol`: Use request body error message for unknown errors missing a message header
6 changes: 4 additions & 2 deletions private/protocol/restjson/unmarshal_error.go
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ func (u *UnmarshalTypedError) UnmarshalError(
msg := resp.Header.Get(errorMessageHeader)

body := resp.Body
if len(code) == 0 {
if len(code) == 0 || len(msg) == 0 {
// If unable to get code from HTTP headers have to parse JSON message
// to determine what kind of exception this will be.
var buf bytes.Buffer
@@ -57,7 +57,9 @@ func (u *UnmarshalTypedError) UnmarshalError(
}

body = ioutil.NopCloser(&buf)
code = jsonErr.Code
if len(code) == 0 {
code = jsonErr.Code
}
Comment on lines +60 to +62
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition was added to preserve backward compatibility and prevent the code from being overwritten if the code header was present.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, this is also in line with both the smithy docs on restjson error type as well as a change we recently made in this area in go v2.

msg = jsonErr.Message
}

26 changes: 26 additions & 0 deletions private/protocol/restjson/unmarshal_error_test.go
Original file line number Diff line number Diff line change
@@ -161,6 +161,32 @@ func TestUnmarshalTypedError(t *testing.T) {
respMeta.RequestID,
),
},
"unknown code header only": {
Response: &http.Response{
Header: http.Header{
errorTypeHeader: []string{"UnknownError"},
},
Body: ioutil.NopCloser(strings.NewReader(unknownErrJSON)),
},
Expect: awserr.NewRequestFailure(
awserr.New("UnknownError", "error message", nil),
respMeta.StatusCode,
respMeta.RequestID,
),
},
"unknown message header only": {
Response: &http.Response{
Header: http.Header{
errorMessageHeader: []string{"overwritten error message"},
},
Body: ioutil.NopCloser(strings.NewReader(unknownErrJSON)),
},
Expect: awserr.NewRequestFailure(
awserr.New("UnknownError", "error message", nil),
respMeta.StatusCode,
respMeta.RequestID,
),
},
"code from header": {
Response: &http.Response{
Header: http.Header{