Handle HTTP status 409 and non-json errors better (#132)
This commit is contained in:
commit
417bfad07a
1 changed files with 5 additions and 1 deletions
|
@ -70,6 +70,8 @@ func (c *Client) getResponse(method, path string, header http.Header, body io.Re
|
|||
return nil, errors.New("403 Forbidden")
|
||||
case 404:
|
||||
return nil, errors.New("404 Not Found")
|
||||
case 409:
|
||||
return nil, errors.New("409 Conflict")
|
||||
case 422:
|
||||
return nil, fmt.Errorf("422 Unprocessable Entity: %s", string(data))
|
||||
}
|
||||
|
@ -77,7 +79,9 @@ func (c *Client) getResponse(method, path string, header http.Header, body io.Re
|
|||
if resp.StatusCode/100 != 2 {
|
||||
errMap := make(map[string]interface{})
|
||||
if err = json.Unmarshal(data, &errMap); err != nil {
|
||||
return nil, err
|
||||
// when the JSON can't be parsed, data was probably empty or a plain string,
|
||||
// so we try to return a helpful error anyway
|
||||
return nil, fmt.Errorf("Unknown API Error: %d %s", resp.StatusCode, string(data))
|
||||
}
|
||||
return nil, errors.New(errMap["message"].(string))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue