Skip to content

Commit

Permalink
fix(googleapi): fill response headers in Error (#1418)
Browse files Browse the repository at this point in the history
Currently googleapi.Error is not populating the Header field with
response headers. We found this to be true across multiple APIs.
This fix ensures that Header is filled in correctly, allowing
easier debugging by callers.
  • Loading branch information
tritone committed Feb 4, 2022
1 parent 6db1fa9 commit 9eaba81
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions googleapi/googleapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func CheckResponse(res *http.Response) error {
jerr.Error.Code = res.StatusCode
}
jerr.Error.Body = string(slurp)
jerr.Error.Header = res.Header
return jerr.Error
}
}
Expand Down
14 changes: 14 additions & 0 deletions googleapi/googleapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,20 @@ Details:
}
]`,
},
{
// Case: Confirm that response headers are propagated to the error.
&http.Response{
StatusCode: http.StatusInternalServerError,
Header: map[string][]string{"key1": {"value1"}, "key2": {"value2", "value3"}},
},
`{"error":{}}`,
&Error{
Code: http.StatusInternalServerError,
Body: `{"error":{}}`,
Header: map[string][]string{"key1": {"value1"}, "key2": {"value2", "value3"}},
},
`googleapi: got HTTP response code 500 with body: {"error":{}}`,
},
}

func TestCheckResponse(t *testing.T) {
Expand Down

0 comments on commit 9eaba81

Please sign in to comment.