Skip to content

Commit 3ff33d4

Browse files
committed
Use switch to determine response errors, in favour of if
1 parent 4344058 commit 3ff33d4

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

client.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,32 +109,29 @@ func (c *Client) Request(v interface{}, method, path string, data interface{}) e
109109
c.DebugLog.Printf("HTTP RESPONSE: %s", string(responseBody))
110110
}
111111

112-
// Status code 500 is a server error and means nothing can be done at this
113-
// point.
114-
if response.StatusCode == http.StatusInternalServerError {
115-
return ErrUnexpectedResponse
116-
}
117-
118-
// Status code 204 is returned for successful DELETE requests. Don't try to
119-
// unmarshal the body: that would return errors.
120-
if response.StatusCode == http.StatusNoContent && response.ContentLength == 0 {
121-
return nil
122-
}
123-
124-
// Status codes 200 and 201 are indicative of being able to convert the
125-
// response body to the struct that was specified.
126-
if response.StatusCode == http.StatusOK || response.StatusCode == http.StatusCreated {
112+
switch response.StatusCode {
113+
case http.StatusOK, http.StatusCreated:
114+
// Status codes 200 and 201 are indicative of being able to convert the
115+
// response body to the struct that was specified.
127116
if err := json.Unmarshal(responseBody, &v); err != nil {
128117
return fmt.Errorf("could not decode response JSON, %s: %v", string(responseBody), err)
129118
}
130119
return nil
131-
}
120+
case http.StatusNoContent:
121+
// Status code 204 is returned for successful DELETE requests. Don't try to
122+
// unmarshal the body: that would return errors.
123+
return nil
124+
case http.StatusInternalServerError:
125+
// Status code 500 is a server error and means nothing can be done at this
126+
// point.
127+
return ErrUnexpectedResponse
128+
default:
129+
// Anything else than a 200/201/204/500 should be a JSON error.
130+
var errorResponse ErrorResponse
131+
if err := json.Unmarshal(responseBody, &errorResponse); err != nil {
132+
return err
133+
}
132134

133-
// Anything else than a 200/201/500 should be a JSON error.
134-
var errorResponse ErrorResponse
135-
if err := json.Unmarshal(responseBody, &errorResponse); err != nil {
136-
return err
135+
return errorResponse
137136
}
138-
139-
return errorResponse
140137
}

0 commit comments

Comments
 (0)