Skip to content

Commit 9eaba81

Browse files
authored
fix(googleapi): fill response headers in Error (#1418)
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.
1 parent 6db1fa9 commit 9eaba81

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

googleapi/googleapi.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ func CheckResponse(res *http.Response) error {
141141
jerr.Error.Code = res.StatusCode
142142
}
143143
jerr.Error.Body = string(slurp)
144+
jerr.Error.Header = res.Header
144145
return jerr.Error
145146
}
146147
}

googleapi/googleapi_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,20 @@ Details:
298298
}
299299
]`,
300300
},
301+
{
302+
// Case: Confirm that response headers are propagated to the error.
303+
&http.Response{
304+
StatusCode: http.StatusInternalServerError,
305+
Header: map[string][]string{"key1": {"value1"}, "key2": {"value2", "value3"}},
306+
},
307+
`{"error":{}}`,
308+
&Error{
309+
Code: http.StatusInternalServerError,
310+
Body: `{"error":{}}`,
311+
Header: map[string][]string{"key1": {"value1"}, "key2": {"value2", "value3"}},
312+
},
313+
`googleapi: got HTTP response code 500 with body: {"error":{}}`,
314+
},
301315
}
302316

303317
func TestCheckResponse(t *testing.T) {

0 commit comments

Comments
 (0)