From 1ba84a64e4def6f3800245c848f7846c7fb3978d Mon Sep 17 00:00:00 2001 From: ddxyq <657023321@qq.com> Date: Sat, 22 Jun 2024 08:47:41 +0800 Subject: [PATCH] Optimize request error handling process (#572) * Optimize request error handling process * add a Respone filed to APIError --- v2/client.go | 15 +++++++++------ v2/common/errors.go | 14 +++++++++++--- v2/delivery/client.go | 15 +++++++++------ v2/futures/client.go | 17 ++++++++++------- v2/options/client.go | 7 ++++--- 5 files changed, 43 insertions(+), 25 deletions(-) diff --git a/v2/client.go b/v2/client.go index 57848d34..b197e87b 100644 --- a/v2/client.go +++ b/v2/client.go @@ -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 @@ -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 @@ -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 } diff --git a/v2/common/errors.go b/v2/common/errors.go index a4490ecb..2328fa53 100644 --- a/v2/common/errors.go +++ b/v2/common/errors.go @@ -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(" code=%d, msg=%s", e.Code, e.Message) + if e.IsValid() { + return fmt.Sprintf(" code=%d, msg=%s", e.Code, e.Message) + } + return fmt.Sprintf(" rsp=%s", string(e.Response)) +} + +func (e APIError) IsValid() bool { + return e.Code != 0 || e.Message != "" } // IsAPIError check if e is an API error diff --git a/v2/delivery/client.go b/v2/delivery/client.go index fecab1c9..42ed372b 100644 --- a/v2/delivery/client.go +++ b/v2/delivery/client.go @@ -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 @@ -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 @@ -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 } diff --git a/v2/futures/client.go b/v2/futures/client.go index ae0b5878..f108b42d 100644 --- a/v2/futures/client.go +++ b/v2/futures/client.go @@ -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 @@ -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 @@ -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 } diff --git a/v2/options/client.go b/v2/options/client.go index f7e1dc77..6473ed0d 100644 --- a/v2/options/client.go +++ b/v2/options/client.go @@ -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 }