Skip to content

Commit f6a7b91

Browse files
committed
client support SetCommonError
1 parent accc5da commit f6a7b91

File tree

6 files changed

+55
-15
lines changed

6 files changed

+55
-15
lines changed

client.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"net/http/cookiejar"
1717
urlpkg "net/url"
1818
"os"
19+
"reflect"
1920
"strings"
2021
"time"
2122
)
@@ -36,23 +37,23 @@ var defaultClient *Client = C()
3637

3738
// Client is the req's http client.
3839
type Client struct {
39-
BaseURL string
40-
PathParams map[string]string
41-
QueryParams urlpkg.Values
42-
Headers http.Header
43-
Cookies []*http.Cookie
44-
FormData urlpkg.Values
45-
DebugLog bool
46-
AllowGetMethodPayload bool
47-
40+
BaseURL string
41+
PathParams map[string]string
42+
QueryParams urlpkg.Values
43+
Headers http.Header
44+
Cookies []*http.Cookie
45+
FormData urlpkg.Values
46+
DebugLog bool
47+
AllowGetMethodPayload bool
48+
trace bool
49+
disableAutoReadResponse bool
50+
commonErrorType reflect.Type
4851
retryOption *retryOption
4952
jsonMarshal func(v interface{}) ([]byte, error)
5053
jsonUnmarshal func(data []byte, v interface{}) error
5154
xmlMarshal func(v interface{}) ([]byte, error)
5255
xmlUnmarshal func(data []byte, v interface{}) error
53-
trace bool
5456
outputDirectory string
55-
disableAutoReadResponse bool
5657
scheme string
5758
log Logger
5859
t *Transport
@@ -72,6 +73,15 @@ func (c *Client) R() *Request {
7273
}
7374
}
7475

76+
// SetCommonError set the common result that response body will be unmarshalled to
77+
// if it is an error response ( status `code >= 400`).
78+
func (c *Client) SetCommonError(err interface{}) *Client {
79+
if err != nil {
80+
c.commonErrorType = util.GetType(err)
81+
}
82+
return c
83+
}
84+
7585
// SetCommonFormDataFromValues set the form data from url.Values for all requests
7686
// which request method allows payload.
7787
func (c *Client) SetCommonFormDataFromValues(data urlpkg.Values) *Client {

client_wrapper.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import (
1010
"time"
1111
)
1212

13+
// SetCommonError is a global wrapper methods which delegated
14+
// to the default client's SetCommonError.
15+
func SetCommonError(err interface{}) *Client {
16+
return defaultClient.SetCommonError(err)
17+
}
18+
1319
// SetCommonFormDataFromValues is a global wrapper methods which delegated
1420
// to the default client's SetCommonFormDataFromValues.
1521
func SetCommonFormDataFromValues(data url.Values) *Client {

client_wrapper_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestGlobalWrapper(t *testing.T) {
2323
form.Add("test", "test")
2424

2525
assertAllNotNil(t,
26+
SetCommonError(nil),
2627
SetCookieJar(nil),
2728
SetDialTLS(nil),
2829
SetDial(nil),

internal/util/util.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ func GetPointer(v interface{}) interface{} {
2727
return reflect.New(vv.Type()).Interface()
2828
}
2929

30+
// GetType return the underlying type.
31+
func GetType(v interface{}) reflect.Type {
32+
return reflect.Indirect(reflect.ValueOf(v)).Type()
33+
}
34+
3035
// CutString slices s around the first instance of sep,
3136
// returning the text before and after sep.
3237
// The found result reports whether sep appears in s.

middleware.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/url"
1212
"os"
1313
"path/filepath"
14+
"reflect"
1415
"strings"
1516
"time"
1617
)
@@ -280,10 +281,18 @@ func parseResponseBody(c *Client, r *Response) (err error) {
280281
r.result = r.Request.Result
281282
}
282283
}
283-
if r.Request.Error != nil && r.IsError() {
284-
err = unmarshalBody(c, r, r.Request.Error)
285-
if err == nil {
286-
r.error = r.Request.Error
284+
if r.IsError() {
285+
if r.Request.Error != nil {
286+
err = unmarshalBody(c, r, r.Request.Error)
287+
if err == nil {
288+
r.error = r.Request.Error
289+
}
290+
} else if c.commonErrorType != nil {
291+
e := reflect.New(c.commonErrorType).Interface()
292+
err = unmarshalBody(c, r, e)
293+
if err == nil {
294+
r.error = e
295+
}
287296
}
288297
}
289298
return

request_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,15 @@ func testError(t *testing.T, c *Client) {
687687
Get("/search")
688688
assertIsError(t, resp, err)
689689
assertEqual(t, 10001, errMsg.ErrorCode)
690+
691+
c.SetCommonError(&errMsg)
692+
resp, err = c.R().
693+
SetQueryParam("username", "").
694+
Get("/search")
695+
assertIsError(t, resp, err)
696+
em, ok := resp.Error().(*ErrorMessage)
697+
assertEqual(t, true, ok)
698+
assertEqual(t, 10000, em.ErrorCode)
690699
}
691700

692701
func TestForm(t *testing.T) {

0 commit comments

Comments
 (0)