Skip to content

Commit 6f54f42

Browse files
committed
add a Respone filed to APIError
1 parent ddae8d3 commit 6f54f42

File tree

5 files changed

+11
-7
lines changed

5 files changed

+11
-7
lines changed

v2/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
489489
c.debug("failed to unmarshal json: %s\n", e)
490490
}
491491
if !apiErr.IsValid() {
492-
return nil, fmt.Errorf("status_code=%v body=%v", res.StatusCode, string(data))
492+
apiErr.Response = data
493493
}
494494
return nil, apiErr
495495
}

v2/common/errors.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ import (
66

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

1314
// Error return error code and message
1415
func (e APIError) Error() string {
15-
return fmt.Sprintf("<APIError> code=%d, msg=%s", e.Code, e.Message)
16+
if e.IsValid() {
17+
return fmt.Sprintf("<APIError> code=%d, msg=%s", e.Code, e.Message)
18+
}
19+
return fmt.Sprintf("<APIError> rsp=%s", string(e.Response))
1620
}
1721

1822
func (e APIError) IsValid() bool {

v2/delivery/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
319319
c.debug("failed to unmarshal json: %s\n", e)
320320
}
321321
if !apiErr.IsValid() {
322-
return nil, fmt.Errorf("status_code=%v body=%v", res.StatusCode, string(data))
322+
apiErr.Response = data
323323
}
324324
return nil, apiErr
325325
}

v2/futures/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
356356
c.debug("failed to unmarshal json: %s\n", e)
357357
}
358358
if !apiErr.IsValid() {
359-
return nil, &res.Header, fmt.Errorf("status_code=%v body=%v", res.StatusCode, string(data))
359+
apiErr.Response = data
360360
}
361361
return nil, &res.Header, apiErr
362362
}

v2/options/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
359359
c.debug("failed to unmarshal json: %s\n", e)
360360
}
361361
if !apiErr.IsValid() {
362-
return nil, &res.Header, fmt.Errorf("status_code=%v body=%v", res.StatusCode, string(data))
362+
apiErr.Response = data
363363
}
364364
return nil, &res.Header, apiErr
365365
}

0 commit comments

Comments
 (0)