Skip to content

Commit

Permalink
fix(api): accept 2xx responses without error
Browse files Browse the repository at this point in the history
Many of our API endpoints that create objects now return 201 Created
instead of 200 OK. The API implementation here was previously only
accepting http.StatusOK as valid.

With this change, the API handler now accepts 2xx status codes.

Bug: #173
  • Loading branch information
terinjokes committed Mar 22, 2018
1 parent 7a8c415 commit f0cb847
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
17 changes: 10 additions & 7 deletions cloudflare.go
Expand Up @@ -109,15 +109,18 @@ func (api *API) makeRequestWithAuthType(method, uri string, params interface{},
return nil, errors.Wrap(err, "could not read response body")
}

switch resp.StatusCode {
case http.StatusOK:
break
case http.StatusUnauthorized:
switch {
case resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices:
case resp.StatusCode == http.StatusUnauthorized:
return nil, errors.Errorf("HTTP status %d: invalid credentials", resp.StatusCode)
case http.StatusForbidden:
case resp.StatusCode == http.StatusForbidden:
return nil, errors.Errorf("HTTP status %d: insufficient permissions", resp.StatusCode)
case http.StatusServiceUnavailable, http.StatusBadGateway, http.StatusGatewayTimeout,
522, 523, 524:
case resp.StatusCode == http.StatusServiceUnavailable,
resp.StatusCode == http.StatusBadGateway,
resp.StatusCode == http.StatusGatewayTimeout,
resp.StatusCode == 522,
resp.StatusCode == 523,
resp.StatusCode == 524:
return nil, errors.Errorf("HTTP status %d: service failure", resp.StatusCode)
default:
var s string
Expand Down
1 change: 1 addition & 0 deletions custom_hostname_test.go
Expand Up @@ -35,6 +35,7 @@ func TestCustomHostname_CreateCustomHostname(t *testing.T) {
assert.Equal(t, "POST", r.Method, "Expected method 'POST', got %s", r.Method)

w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"success": true,
Expand Down

0 comments on commit f0cb847

Please sign in to comment.