Skip to content

Commit

Permalink
[feat] Add Error Context to HTTP 400 errors (#24)
Browse files Browse the repository at this point in the history
* [feat] Add Error Context to HTTP 400 errors
* [build] Add Go 1.10.x to Travis CI config
  • Loading branch information
elithrar authored Apr 11, 2018
1 parent 6f05974 commit 7b01169
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ matrix:
include:
- go: 1.x
env: LATEST=true
- go: 1.7
- go: 1.8
- go: 1.9
- go: 1.7.x
- go: 1.8.x
- go: 1.9.x
- go: 1.10.x
- go: tip
allow_failures:
- go: tip
Expand Down
10 changes: 9 additions & 1 deletion logshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -208,7 +209,14 @@ func (c *Client) request(u *url.URL) (*Meta, error) {
}

if resp.StatusCode < 200 || resp.StatusCode > 299 {
return meta, errors.Errorf("HTTP status %d: request failed", resp.StatusCode)
// Read errors, but provide a cap on total read size for safety.
lr := io.LimitReader(resp.Body, 1000000)
body, err := ioutil.ReadAll(lr)
if err != nil {
return meta, errors.Wrapf(err, "HTTP status %d: request failed", resp.StatusCode)
}

return meta, errors.Errorf("HTTP status %d: request failed: %s", resp.StatusCode, body)
}

// Explicitly handle the 204 No Content case.
Expand Down

0 comments on commit 7b01169

Please sign in to comment.