Skip to content

Commit

Permalink
fix: parse the error message
Browse files Browse the repository at this point in the history
  • Loading branch information
knight42 committed Jan 19, 2018
1 parent 413e380 commit cbef7f6
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -946,12 +946,20 @@ type Error struct {
}

func newError(resp *http.Response) *Error {
type ErrMsg 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)}
var emsg ErrMsg
err = json.Unmarshal(data, &emsg)
if err != nil {
return &Error{Status: resp.StatusCode, Message: string(data)}
}
return &Error{Status: resp.StatusCode, Message: emsg.Message}
}

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

0 comments on commit cbef7f6

Please sign in to comment.