Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize request error handling process #572

Merged
merged 2 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
return nil, fmt.Errorf("status_code=%v body=%v", res.StatusCode, string(data))
xyq-c-cpp marked this conversation as resolved.
Show resolved Hide resolved
}
return nil, apiErr
}
Expand Down
4 changes: 4 additions & 0 deletions v2/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func (e APIError) Error() string {
return fmt.Sprintf("<APIError> code=%d, msg=%s", e.Code, e.Message)
}

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

// IsAPIError check if e is an API error
func IsAPIError(e error) bool {
_, ok := e.(*APIError)
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() {
return nil, fmt.Errorf("status_code=%v body=%v", res.StatusCode, string(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() {
return nil, &res.Header, fmt.Errorf("status_code=%v body=%v", res.StatusCode, string(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() {
return nil, &res.Header, fmt.Errorf("status_code=%v body=%v", res.StatusCode, string(data))
}
return nil, &res.Header, apiErr
}
return data, &res.Header, nil
}
Expand Down
Loading