mirror of
https://github.com/c9s/bbgo.git
synced 2024-09-20 08:11:08 +00:00
util: extract IsError method
This commit is contained in:
parent
2e82b33568
commit
565086cc2a
|
@ -274,7 +274,7 @@ func (c *RestClient) sendRequest(req *http.Request) (*util.Response, error) {
|
|||
}
|
||||
|
||||
// Check error, if there is an error, return the ErrorResponse struct type
|
||||
if isError(response) {
|
||||
if response.IsError() {
|
||||
errorResponse, err := toErrorResponse(response)
|
||||
if err != nil {
|
||||
return response, err
|
||||
|
@ -359,12 +359,6 @@ func (r *ErrorResponse) Error() string {
|
|||
)
|
||||
}
|
||||
|
||||
// isError check the response status code so see if a response is an error.
|
||||
func isError(response *util.Response) bool {
|
||||
var c = response.StatusCode
|
||||
return c < 200 || c > 299
|
||||
}
|
||||
|
||||
// toErrorResponse tries to convert/parse the server response to the standard Error interface object
|
||||
func toErrorResponse(response *util.Response) (errorResponse *ErrorResponse, err error) {
|
||||
errorResponse = &ErrorResponse{Response: response}
|
||||
|
|
|
@ -36,3 +36,7 @@ func (r *Response) String() string {
|
|||
func (r *Response) DecodeJSON(o interface{}) error {
|
||||
return json.Unmarshal(r.Body, o)
|
||||
}
|
||||
|
||||
func (r *Response) IsError() bool {
|
||||
return r.StatusCode >= 400
|
||||
}
|
||||
|
|
|
@ -26,3 +26,19 @@ func TestResponse_DecodeJSON(t *testing.T) {
|
|||
assert.NoError(t, resp.DecodeJSON(&result))
|
||||
assert.Equal(t, "Test Name", result.Name)
|
||||
}
|
||||
|
||||
func TestResponse_IsError(t *testing.T) {
|
||||
resp := &Response{Response: &http.Response{}}
|
||||
cases := map[int]bool{
|
||||
100: false,
|
||||
200: false,
|
||||
300: false,
|
||||
400: true,
|
||||
500: true,
|
||||
}
|
||||
|
||||
for code, isErr := range cases {
|
||||
resp.StatusCode = code
|
||||
assert.Equal(t, isErr, resp.IsError())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user