Skip to content

Commit

Permalink
Refactor response errors on client request (#162)
Browse files Browse the repository at this point in the history
* refactor response errors

* catch potential response body close error on request

---------

Co-authored-by: Rostislav Lyupa <>
Co-authored-by: hirokisan <dev@hirokiueno.com>
  • Loading branch information
lyro41 and hirokisan authored Feb 2, 2024
1 parent a3fd779 commit 41b4c7c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
17 changes: 12 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,20 @@ func (c *Client) WithBaseURL(url string) *Client {
}

// Request :
func (c *Client) Request(req *http.Request, dst interface{}) error {
func (c *Client) Request(req *http.Request, dst interface{}) (err error) {
c.debugf("request: %v", req)
resp, err := c.httpClient.Do(req)
c.debugf("response: %v", resp)
c.debugf("response status code: %v", resp.StatusCode)
if err != nil {
return err
}
defer resp.Body.Close()
defer func() {
cerr := resp.Body.Close()
if err == nil && cerr != nil {
err = cerr
}
}()

switch {
case 200 <= resp.StatusCode && resp.StatusCode <= 299:
Expand All @@ -132,12 +137,14 @@ func (c *Client) Request(req *http.Request, dst interface{}) error {

c.debugf("response body: %v", string(body))
return nil
case resp.StatusCode == http.StatusBadRequest:
return fmt.Errorf("%v: Need to send the request with GET / POST (must be capitalized)", ErrBadRequest)
case resp.StatusCode == http.StatusUnauthorized:
return fmt.Errorf("%w: invalid key/secret", ErrAccessDenied)
return fmt.Errorf("%w: invalid key/secret", ErrInvalidRequest)
case resp.StatusCode == http.StatusForbidden:
return fmt.Errorf("%w: not permitted", ErrAccessDenied)
return fmt.Errorf("%w: not permitted", ErrForbiddenRequest)
case resp.StatusCode == http.StatusNotFound:
return ErrPathNotFound
return fmt.Errorf("%w: wrong path", ErrPathNotFound)
default:
return fmt.Errorf("unexpected status code %d", resp.StatusCode)
}
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func TestClient(t *testing.T) {
var got interface{}

gotErr := client.Request(req, &got)
assert.ErrorIs(t, gotErr, bybit.ErrAccessDenied)
assert.ErrorIs(t, gotErr, bybit.ErrForbiddenRequest)
})
t.Run("404, path not found", func(t *testing.T) {
path := "/test"
Expand Down
20 changes: 17 additions & 3 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,22 @@ func (r *RateLimitV5Error) Error() string {
}

var (
// ErrPathNotFound : Request path not found
// ErrBadRequest :
// Need to send the request with GET / POST (must be capitalized)
ErrBadRequest = errors.New("bad request")
// ErrInvalidRequest :
// 1. Need to use the correct key to access;
// 2. Need to put authentication params in the request header
ErrInvalidRequest = errors.New("authentication failed")
// ErrForbiddenRequest :
// Possible causes:
// 1. IP rate limit breached;
// 2. You send GET request with an empty json body;
// 3. You are using U.S IP
ErrForbiddenRequest = errors.New("access denied")
// ErrPathNotFound :
// Possible causes:
// 1. Wrong path;
// 2. Category value does not match account mode
ErrPathNotFound = errors.New("path not found")
// ErrAccessDenied : Access denied
ErrAccessDenied = errors.New("access denied")
)

0 comments on commit 41b4c7c

Please sign in to comment.