Skip to content

Commit

Permalink
Merge pull request #18 from storageos/feature/DEV-1617-update-user-po…
Browse files Browse the repository at this point in the history
…licy-endpoint-to-conform

Support the json error messages
  • Loading branch information
JoeReid authored Aug 23, 2017
2 parents f3d9bca + 976b8e1 commit 823afe4
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 823afe4

Please sign in to comment.