Skip to content

Commit

Permalink
Fix nil deref on strange IAM error response
Browse files Browse the repository at this point in the history
Apparently IAM is sending back valid JSON without
a context object in some cases, and we don't check for
that being nil, which causes a nil deref issue sometimes.

Add code to check and handle, and a unit test.

Resolves: #45
  • Loading branch information
mrodden committed Oct 16, 2020
1 parent a0492ea commit d2c4d08
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 12 additions & 1 deletion iam/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ type iamRequestContext struct {
}

func (ie Error) Error() string {

reqId := ""
if ie.Context != nil {
reqId = ie.Context.RequestID
}

statusCode := 0
if ie.HTTPResponse != nil {
statusCode = ie.HTTPResponse.StatusCode
}

return fmt.Sprintf("iam.Error: HTTP %d requestId='%s' message='%s %s'",
ie.HTTPResponse.StatusCode, ie.Context.RequestID, ie.ErrorCode, ie.ErrorMessage)
statusCode, reqId, ie.ErrorCode, ie.ErrorMessage)
}
14 changes: 14 additions & 0 deletions iam/iam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,17 @@ func TestValid_NilToken_ReturnsFalse(t *testing.T) {
var tok *Token
assert.False(t, tok.Valid(), "tok.Valid() should return false")
}

func TestError_NilContextOrResp_NoNilDeref(t *testing.T) {

err := Error{
ErrorCode: "TEST123",
ErrorMessage: "Test Error",
Context: nil,
HTTPResponse: nil,
}

errStr := err.Error()

assert.Equal(t, errStr, "iam.Error: HTTP 0 requestId='' message='TEST123 Test Error'")
}

0 comments on commit d2c4d08

Please sign in to comment.