(op string, resp *http.Response)
| 219 | } |
| 220 | |
| 221 | func (r *RESTClient) statusCodeToError(op string, resp *http.Response) error { |
| 222 | if resp.Header.Get("Content-Type") == "application/json" { |
| 223 | var errorsResp response |
| 224 | if json.NewDecoder(resp.Body).Decode(&errorsResp) == nil { |
| 225 | if err := errorsResp.checkErrors(); err != nil { |
| 226 | return errors.Errorf("Failed to %s: %s", op, err) |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | switch resp.StatusCode { |
| 232 | case http.StatusOK: |
| 233 | return nil |
| 234 | case http.StatusBadRequest: |
| 235 | return ErrBadRequest |
| 236 | case http.StatusUnauthorized, http.StatusForbidden: |
| 237 | return ErrUnauthorized |
| 238 | case http.StatusNotFound: |
| 239 | return ErrNotFound |
| 240 | } |
| 241 | return errors.Errorf("API call to %s failed with status %d: %s", op, |
| 242 | resp.StatusCode, http.StatusText(resp.StatusCode)) |
| 243 | } |
no test coverage detected