Skip to content

Commit ddae8d3

Browse files
committed
Optimize request error handling process
1 parent 418ff24 commit ddae8d3

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

v2/client.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ func (c *Client) parseRequest(r *request, opts ...RequestOption) (err error) {
438438
if queryString != "" {
439439
fullURL = fmt.Sprintf("%s?%s", fullURL, queryString)
440440
}
441-
c.debug("full url: %s, body: %s", fullURL, bodyString)
441+
c.debug("full url: %s, body: %s\n", fullURL, bodyString)
442442

443443
r.fullURL = fullURL
444444
r.header = header
@@ -457,7 +457,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
457457
}
458458
req = req.WithContext(ctx)
459459
req.Header = r.header
460-
c.debug("request: %#v", req)
460+
c.debug("request: %#v\n", req)
461461
f := c.do
462462
if f == nil {
463463
f = c.HTTPClient.Do
@@ -478,15 +478,18 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
478478
err = cerr
479479
}
480480
}()
481-
c.debug("response: %#v", res)
482-
c.debug("response body: %s", string(data))
483-
c.debug("response status code: %d", res.StatusCode)
481+
c.debug("response: %#v\n", res)
482+
c.debug("response body: %s\n", string(data))
483+
c.debug("response status code: %d\n", res.StatusCode)
484484

485485
if res.StatusCode >= http.StatusBadRequest {
486486
apiErr := new(common.APIError)
487487
e := json.Unmarshal(data, apiErr)
488488
if e != nil {
489-
c.debug("failed to unmarshal json: %s", e)
489+
c.debug("failed to unmarshal json: %s\n", e)
490+
}
491+
if !apiErr.IsValid() {
492+
return nil, fmt.Errorf("status_code=%v body=%v", res.StatusCode, string(data))
490493
}
491494
return nil, apiErr
492495
}

v2/common/errors.go

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ func (e APIError) Error() string {
1515
return fmt.Sprintf("<APIError> code=%d, msg=%s", e.Code, e.Message)
1616
}
1717

18+
func (e APIError) IsValid() bool {
19+
return e.Code != 0 || e.Message != ""
20+
}
21+
1822
// IsAPIError check if e is an API error
1923
func IsAPIError(e error) bool {
2024
_, ok := e.(*APIError)

v2/delivery/client.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (c *Client) parseRequest(r *request, opts ...RequestOption) (err error) {
268268
if queryString != "" {
269269
fullURL = fmt.Sprintf("%s?%s", fullURL, queryString)
270270
}
271-
c.debug("full url: %s, body: %s", fullURL, bodyString)
271+
c.debug("full url: %s, body: %s\n", fullURL, bodyString)
272272

273273
r.fullURL = fullURL
274274
r.header = header
@@ -287,7 +287,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
287287
}
288288
req = req.WithContext(ctx)
289289
req.Header = r.header
290-
c.debug("request: %#v", req)
290+
c.debug("request: %#v\n", req)
291291
f := c.do
292292
if f == nil {
293293
f = c.HTTPClient.Do
@@ -308,15 +308,18 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
308308
err = cerr
309309
}
310310
}()
311-
c.debug("response: %#v", res)
312-
c.debug("response body: %s", string(data))
313-
c.debug("response status code: %d", res.StatusCode)
311+
c.debug("response: %#v\n", res)
312+
c.debug("response body: %s\n", string(data))
313+
c.debug("response status code: %d\n", res.StatusCode)
314314

315315
if res.StatusCode >= http.StatusBadRequest {
316316
apiErr := new(common.APIError)
317317
e := json.Unmarshal(data, apiErr)
318318
if e != nil {
319-
c.debug("failed to unmarshal json: %s", e)
319+
c.debug("failed to unmarshal json: %s\n", e)
320+
}
321+
if !apiErr.IsValid() {
322+
return nil, fmt.Errorf("status_code=%v body=%v", res.StatusCode, string(data))
320323
}
321324
return nil, apiErr
322325
}

v2/futures/client.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func (c *Client) parseRequest(r *request, opts ...RequestOption) (err error) {
305305
if queryString != "" {
306306
fullURL = fmt.Sprintf("%s?%s", fullURL, queryString)
307307
}
308-
c.debug("full url: %s, body: %s", fullURL, bodyString)
308+
c.debug("full url: %s, body: %s\n", fullURL, bodyString)
309309

310310
r.fullURL = fullURL
311311
r.header = header
@@ -324,7 +324,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
324324
}
325325
req = req.WithContext(ctx)
326326
req.Header = r.header
327-
c.debug("request: %#v", req)
327+
c.debug("request: %#v\n", req)
328328
f := c.do
329329
if f == nil {
330330
f = c.HTTPClient.Do
@@ -345,17 +345,20 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
345345
err = cerr
346346
}
347347
}()
348-
c.debug("response: %#v", res)
349-
c.debug("response body: %s", string(data))
350-
c.debug("response status code: %d", res.StatusCode)
348+
c.debug("response: %#v\n", res)
349+
c.debug("response body: %s\n", string(data))
350+
c.debug("response status code: %d\n", res.StatusCode)
351351

352352
if res.StatusCode >= http.StatusBadRequest {
353353
apiErr := new(common.APIError)
354354
e := json.Unmarshal(data, apiErr)
355355
if e != nil {
356-
c.debug("failed to unmarshal json: %s", e)
356+
c.debug("failed to unmarshal json: %s\n", e)
357357
}
358-
return nil, &http.Header{}, apiErr
358+
if !apiErr.IsValid() {
359+
return nil, &res.Header, fmt.Errorf("status_code=%v body=%v", res.StatusCode, string(data))
360+
}
361+
return nil, &res.Header, apiErr
359362
}
360363
return data, &res.Header, nil
361364
}

v2/options/client.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,11 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
357357
e := json.Unmarshal(data, apiErr)
358358
if e != nil {
359359
c.debug("failed to unmarshal json: %s\n", e)
360-
apiErr.Code = int64(res.StatusCode)
361-
apiErr.Message = string(data)
362360
}
363-
return nil, &http.Header{}, apiErr
361+
if !apiErr.IsValid() {
362+
return nil, &res.Header, fmt.Errorf("status_code=%v body=%v", res.StatusCode, string(data))
363+
}
364+
return nil, &res.Header, apiErr
364365
}
365366
return data, &res.Header, nil
366367
}

0 commit comments

Comments
 (0)