From 70dcb4084d3cd1cfad89a5e11d8540117f27135f Mon Sep 17 00:00:00 2001 From: Will Norris Date: Tue, 28 May 2013 18:23:50 -0700 Subject: [PATCH] drop errors caused from reading error response body if we have an error reading the body of an API error response, still return an ErrorResposne with the original response struct. Simply returning a string stating that we couldn't read the response body doesn't give the caller anything actionable to work with or attempt to process. --- github/github.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/github/github.go b/github/github.go index 52dd533..b383522 100644 --- a/github/github.go +++ b/github/github.go @@ -200,21 +200,19 @@ func (e *Error) Error() string { } // CheckResponse checks the API response for errors, and returns them if -// present. +// present. API error responses are expected to have either no response body, +// or a JSON response body that maps to ErrorResponse. Any other response body +// will be silently ignored. func CheckResponse(r *http.Response) error { if c := r.StatusCode; 200 <= c && c <= 299 { return nil } + errorResponse := &ErrorResponse{Response: r} data, err := ioutil.ReadAll(r.Body) - if err == nil { - errorResponse := &ErrorResponse{Response: r} - if data != nil { - err = json.Unmarshal(data, errorResponse) - } - return errorResponse + if err == nil && data != nil { + json.Unmarshal(data, errorResponse) } - return fmt.Errorf("github: got HTTP response code %d and error reading body: %v", - r.StatusCode, err) + return errorResponse } // API response wrapper to a rate limit request.