Skip to content

Commit

Permalink
Fix HTTP error handling in RESTClient.Request() method
Browse files Browse the repository at this point in the history
- Ensure that `Request()` calls HandleHTTPError so that any JSON error body properly gets parsed;
- Ensure that response body is closed after reading the error response.
  • Loading branch information
mislav committed Oct 13, 2022
1 parent 05415b9 commit a97c3d8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 10 deletions.
4 changes: 0 additions & 4 deletions example_gh_test.go
Expand Up @@ -77,10 +77,6 @@ func ExampleRESTClient_request() {
}
defer resp.Body.Close()

if resp.StatusCode > 299 {
log.Fatal("server error")
}

f, err := os.CreateTemp("", "*_checksums.txt")
if err != nil {
log.Fatal(err)
Expand Down
9 changes: 3 additions & 6 deletions internal/api/rest_client.go
Expand Up @@ -33,16 +33,13 @@ func (c restClient) RequestWithContext(ctx context.Context, method string, path

resp, err := c.client.Do(req)
if err != nil {
return resp, err
return nil, err
}

success := resp.StatusCode >= 200 && resp.StatusCode < 300
if !success {
err = api.HTTPError{
Headers: resp.Header,
RequestURL: resp.Request.URL,
StatusCode: resp.StatusCode,
}
defer resp.Body.Close()
return nil, api.HandleHTTPError(resp)
}

return resp, err
Expand Down

0 comments on commit a97c3d8

Please sign in to comment.