Skip to content

Commit 9e749c4

Browse files
bjohnso5codyoss
andauthored
feat: add error helper that does not read the response body (#2964)
Co-authored-by: Cody Oss <6331106+codyoss@users.noreply.github.com>
1 parent 62f95ef commit 9e749c4

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

‎googleapi/googleapi.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,40 @@ func CheckResponse(res *http.Response) error {
145145
}
146146
slurp, err := io.ReadAll(res.Body)
147147
if err == nil {
148-
jerr := new(errorReply)
149-
err = json.Unmarshal(slurp, jerr)
150-
if err == nil && jerr.Error != nil {
151-
if jerr.Error.Code == 0 {
152-
jerr.Error.Code = res.StatusCode
153-
}
154-
jerr.Error.Body = string(slurp)
155-
jerr.Error.Header = res.Header
156-
return jerr.Error
157-
}
148+
return CheckResponseWithBody(res, slurp)
158149
}
159150
return &Error{
160151
Code: res.StatusCode,
161152
Body: string(slurp),
162153
Header: res.Header,
163154
}
155+
156+
}
157+
158+
// CheckResponseWithBody returns an error (of type *Error) if the response
159+
// status code is not 2xx. Distinct from CheckResponse to allow for checking
160+
// a previously-read body to maintain error detail content.
161+
func CheckResponseWithBody(res *http.Response, body []byte) error {
162+
if res.StatusCode >= 200 && res.StatusCode <= 299 {
163+
return nil
164+
}
165+
166+
jerr := new(errorReply)
167+
err := json.Unmarshal(body, jerr)
168+
if err == nil && jerr.Error != nil {
169+
if jerr.Error.Code == 0 {
170+
jerr.Error.Code = res.StatusCode
171+
}
172+
jerr.Error.Body = string(body)
173+
jerr.Error.Header = res.Header
174+
return jerr.Error
175+
}
176+
177+
return &Error{
178+
Code: res.StatusCode,
179+
Body: string(body),
180+
Header: res.Header,
181+
}
164182
}
165183

166184
// IsNotModified reports whether err is the result of the

‎googleapi/googleapi_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,32 @@ func TestCheckResponse(t *testing.T) {
339339
}
340340
}
341341

342+
func TestCheckResponseWithBody(t *testing.T) {
343+
for _, test := range checkResponseTests {
344+
res := test.in
345+
var body []byte
346+
if test.bodyText != "" {
347+
body = []byte(test.bodyText)
348+
}
349+
g := CheckResponseWithBody(res, body)
350+
if !reflect.DeepEqual(g, test.want) {
351+
t.Errorf("CheckResponse: got %v, want %v", g, test.want)
352+
gotJSON, err := json.Marshal(g)
353+
if err != nil {
354+
t.Error(err)
355+
}
356+
wantJSON, err := json.Marshal(test.want)
357+
if err != nil {
358+
t.Error(err)
359+
}
360+
t.Errorf("json(got): %q\njson(want): %q", string(gotJSON), string(wantJSON))
361+
}
362+
if g != nil && g.Error() != test.errText {
363+
t.Errorf("CheckResponse: unexpected error message.\nGot: %q\nwant: %q", g, test.errText)
364+
}
365+
}
366+
}
367+
342368
type VariantPoint struct {
343369
Type string
344370
Coordinates []float64

0 commit comments

Comments
 (0)