Browse Source

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.
Will Norris 13 years ago
parent
commit
70dcb4084d
1 changed files with 7 additions and 9 deletions
  1. +7
    -9
      github/github.go

+ 7
- 9
github/github.go View File

@ -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.


Loading…
Cancel
Save