Skip to content

Commit

Permalink
Fix non-retriable HTTP error handling
Browse files Browse the repository at this point in the history
Read the response body and not the request. Original version always panics with a nil pointer deref as `req.Body` is always `nil`
  • Loading branch information
ORBAT authored and atombender committed Nov 11, 2019
1 parent 49682db commit e9d9296
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,19 @@ func (c *Client) performGET(
}

func (c *Client) handleErrorResponse(req *http.Request, resp *http.Response) error {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
body = []byte(fmt.Sprintf("[failed to read response body: %s]", err))
responseBody := []byte("<no response body>")

if resp.Body != nil {
var err error
if responseBody, err = ioutil.ReadAll(resp.Body); err != nil {
responseBody = []byte(fmt.Sprintf("[failed to read response body: %s]", err))
}
}

return &RequestError{
Request: req,
Response: resp,
Body: body,
Body: responseBody,
}
}

Expand Down

0 comments on commit e9d9296

Please sign in to comment.