Skip to content

Commit

Permalink
Support the json error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Reid committed Aug 22, 2017
1 parent f3d9bca commit 976b8e1
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,12 +530,24 @@ type Error struct {
}

func newError(resp *http.Response) *Error {
type jsonError struct {
Message string `json:"message"`
}

defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &Error{Status: resp.StatusCode, Message: fmt.Sprintf("cannot read body, err: %v", err)}
}
return &Error{Status: resp.StatusCode, Message: string(data)}

// attempt to unmarshal the error if in json format
jerr := &jsonError{}
err = json.Unmarshal(data, jerr)
if err != nil {
return &Error{Status: resp.StatusCode, Message: string(data)} // Failed, just return string
}

return &Error{Status: resp.StatusCode, Message: jerr.Message}
}

func (e *Error) Error() string {
Expand Down

0 comments on commit 976b8e1

Please sign in to comment.