Skip to content

Commit

Permalink
Return HTTPError from Send
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmccabe committed Aug 6, 2018
1 parent 4a372aa commit 0109ae9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 1 addition & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
Expand Down Expand Up @@ -47,7 +46,7 @@ func (c *BaseClient) Send(req *http.Request, v interface{}) error {
defer resp.Body.Close()

if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("%d", resp.StatusCode)
return httpError(req, resp)
}

if v != nil {
Expand Down
24 changes: 24 additions & 0 deletions client/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package client

import (
"fmt"
"net/http"
)

type HTTPError struct {
status string
StatusCode int
url string
}

func httpError(req *http.Request, resp *http.Response) error {
return &HTTPError{
status: resp.Status,
StatusCode: resp.StatusCode,
url: req.URL.String(),
}
}

func (e *HTTPError) Error() string {
return fmt.Sprintf("%s: %s", e.url, e.status)
}

0 comments on commit 0109ae9

Please sign in to comment.