Skip to content

Commit

Permalink
Optimize request error handling process (adshao#572)
Browse files Browse the repository at this point in the history
* Optimize request error handling process

* add a Respone filed to APIError
  • Loading branch information
xyq-c-cpp authored Jun 22, 2024
1 parent 418ff24 commit 1ba84a6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 25 deletions.
15 changes: 9 additions & 6 deletions v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func (c *Client) parseRequest(r *request, opts ...RequestOption) (err error) {
if queryString != "" {
fullURL = fmt.Sprintf("%s?%s", fullURL, queryString)
}
c.debug("full url: %s, body: %s", fullURL, bodyString)
c.debug("full url: %s, body: %s\n", fullURL, bodyString)

r.fullURL = fullURL
r.header = header
Expand All @@ -457,7 +457,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
}
req = req.WithContext(ctx)
req.Header = r.header
c.debug("request: %#v", req)
c.debug("request: %#v\n", req)
f := c.do
if f == nil {
f = c.HTTPClient.Do
Expand All @@ -478,15 +478,18 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
err = cerr
}
}()
c.debug("response: %#v", res)
c.debug("response body: %s", string(data))
c.debug("response status code: %d", res.StatusCode)
c.debug("response: %#v\n", res)
c.debug("response body: %s\n", string(data))
c.debug("response status code: %d\n", res.StatusCode)

if res.StatusCode >= http.StatusBadRequest {
apiErr := new(common.APIError)
e := json.Unmarshal(data, apiErr)
if e != nil {
c.debug("failed to unmarshal json: %s", e)
c.debug("failed to unmarshal json: %s\n", e)
}
if !apiErr.IsValid() {
apiErr.Response = data
}
return nil, apiErr
}
Expand Down
14 changes: 11 additions & 3 deletions v2/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ import (

// APIError define API error when response status is 4xx or 5xx
type APIError struct {
Code int64 `json:"code"`
Message string `json:"msg"`
Code int64 `json:"code"`
Message string `json:"msg"`
Response []byte `json:"-"` // Assign the body value when the Code and Message fields are invalid.
}

// Error return error code and message
func (e APIError) Error() string {
return fmt.Sprintf("<APIError> code=%d, msg=%s", e.Code, e.Message)
if e.IsValid() {
return fmt.Sprintf("<APIError> code=%d, msg=%s", e.Code, e.Message)
}
return fmt.Sprintf("<APIError> rsp=%s", string(e.Response))
}

func (e APIError) IsValid() bool {
return e.Code != 0 || e.Message != ""
}

// IsAPIError check if e is an API error
Expand Down
15 changes: 9 additions & 6 deletions v2/delivery/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (c *Client) parseRequest(r *request, opts ...RequestOption) (err error) {
if queryString != "" {
fullURL = fmt.Sprintf("%s?%s", fullURL, queryString)
}
c.debug("full url: %s, body: %s", fullURL, bodyString)
c.debug("full url: %s, body: %s\n", fullURL, bodyString)

r.fullURL = fullURL
r.header = header
Expand All @@ -287,7 +287,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
}
req = req.WithContext(ctx)
req.Header = r.header
c.debug("request: %#v", req)
c.debug("request: %#v\n", req)
f := c.do
if f == nil {
f = c.HTTPClient.Do
Expand All @@ -308,15 +308,18 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
err = cerr
}
}()
c.debug("response: %#v", res)
c.debug("response body: %s", string(data))
c.debug("response status code: %d", res.StatusCode)
c.debug("response: %#v\n", res)
c.debug("response body: %s\n", string(data))
c.debug("response status code: %d\n", res.StatusCode)

if res.StatusCode >= http.StatusBadRequest {
apiErr := new(common.APIError)
e := json.Unmarshal(data, apiErr)
if e != nil {
c.debug("failed to unmarshal json: %s", e)
c.debug("failed to unmarshal json: %s\n", e)
}
if !apiErr.IsValid() {
apiErr.Response = data
}
return nil, apiErr
}
Expand Down
17 changes: 10 additions & 7 deletions v2/futures/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func (c *Client) parseRequest(r *request, opts ...RequestOption) (err error) {
if queryString != "" {
fullURL = fmt.Sprintf("%s?%s", fullURL, queryString)
}
c.debug("full url: %s, body: %s", fullURL, bodyString)
c.debug("full url: %s, body: %s\n", fullURL, bodyString)

r.fullURL = fullURL
r.header = header
Expand All @@ -324,7 +324,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
}
req = req.WithContext(ctx)
req.Header = r.header
c.debug("request: %#v", req)
c.debug("request: %#v\n", req)
f := c.do
if f == nil {
f = c.HTTPClient.Do
Expand All @@ -345,17 +345,20 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
err = cerr
}
}()
c.debug("response: %#v", res)
c.debug("response body: %s", string(data))
c.debug("response status code: %d", res.StatusCode)
c.debug("response: %#v\n", res)
c.debug("response body: %s\n", string(data))
c.debug("response status code: %d\n", res.StatusCode)

if res.StatusCode >= http.StatusBadRequest {
apiErr := new(common.APIError)
e := json.Unmarshal(data, apiErr)
if e != nil {
c.debug("failed to unmarshal json: %s", e)
c.debug("failed to unmarshal json: %s\n", e)
}
return nil, &http.Header{}, apiErr
if !apiErr.IsValid() {
apiErr.Response = data
}
return nil, &res.Header, apiErr
}
return data, &res.Header, nil
}
Expand Down
7 changes: 4 additions & 3 deletions v2/options/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,11 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
e := json.Unmarshal(data, apiErr)
if e != nil {
c.debug("failed to unmarshal json: %s\n", e)
apiErr.Code = int64(res.StatusCode)
apiErr.Message = string(data)
}
return nil, &http.Header{}, apiErr
if !apiErr.IsValid() {
apiErr.Response = data
}
return nil, &res.Header, apiErr
}
return data, &res.Header, nil
}
Expand Down

0 comments on commit 1ba84a6

Please sign in to comment.