Skip to content

Commit

Permalink
client support SetCommonError
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Jun 2, 2022
1 parent accc5da commit f6a7b91
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
32 changes: 21 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"net/http/cookiejar"
urlpkg "net/url"
"os"
"reflect"
"strings"
"time"
)
Expand All @@ -36,23 +37,23 @@ var defaultClient *Client = C()

// Client is the req's http client.
type Client struct {
BaseURL string
PathParams map[string]string
QueryParams urlpkg.Values
Headers http.Header
Cookies []*http.Cookie
FormData urlpkg.Values
DebugLog bool
AllowGetMethodPayload bool

BaseURL string
PathParams map[string]string
QueryParams urlpkg.Values
Headers http.Header
Cookies []*http.Cookie
FormData urlpkg.Values
DebugLog bool
AllowGetMethodPayload bool
trace bool
disableAutoReadResponse bool
commonErrorType reflect.Type
retryOption *retryOption
jsonMarshal func(v interface{}) ([]byte, error)
jsonUnmarshal func(data []byte, v interface{}) error
xmlMarshal func(v interface{}) ([]byte, error)
xmlUnmarshal func(data []byte, v interface{}) error
trace bool
outputDirectory string
disableAutoReadResponse bool
scheme string
log Logger
t *Transport
Expand All @@ -72,6 +73,15 @@ func (c *Client) R() *Request {
}
}

// SetCommonError set the common result that response body will be unmarshalled to
// if it is an error response ( status `code >= 400`).
func (c *Client) SetCommonError(err interface{}) *Client {
if err != nil {
c.commonErrorType = util.GetType(err)
}
return c
}

// SetCommonFormDataFromValues set the form data from url.Values for all requests
// which request method allows payload.
func (c *Client) SetCommonFormDataFromValues(data urlpkg.Values) *Client {
Expand Down
6 changes: 6 additions & 0 deletions client_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (
"time"
)

// SetCommonError is a global wrapper methods which delegated
// to the default client's SetCommonError.
func SetCommonError(err interface{}) *Client {
return defaultClient.SetCommonError(err)
}

// SetCommonFormDataFromValues is a global wrapper methods which delegated
// to the default client's SetCommonFormDataFromValues.
func SetCommonFormDataFromValues(data url.Values) *Client {
Expand Down
1 change: 1 addition & 0 deletions client_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestGlobalWrapper(t *testing.T) {
form.Add("test", "test")

assertAllNotNil(t,
SetCommonError(nil),
SetCookieJar(nil),
SetDialTLS(nil),
SetDial(nil),
Expand Down
5 changes: 5 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func GetPointer(v interface{}) interface{} {
return reflect.New(vv.Type()).Interface()
}

// GetType return the underlying type.
func GetType(v interface{}) reflect.Type {
return reflect.Indirect(reflect.ValueOf(v)).Type()
}

// CutString slices s around the first instance of sep,
// returning the text before and after sep.
// The found result reports whether sep appears in s.
Expand Down
17 changes: 13 additions & 4 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/url"
"os"
"path/filepath"
"reflect"
"strings"
"time"
)
Expand Down Expand Up @@ -280,10 +281,18 @@ func parseResponseBody(c *Client, r *Response) (err error) {
r.result = r.Request.Result
}
}
if r.Request.Error != nil && r.IsError() {
err = unmarshalBody(c, r, r.Request.Error)
if err == nil {
r.error = r.Request.Error
if r.IsError() {
if r.Request.Error != nil {
err = unmarshalBody(c, r, r.Request.Error)
if err == nil {
r.error = r.Request.Error
}
} else if c.commonErrorType != nil {
e := reflect.New(c.commonErrorType).Interface()
err = unmarshalBody(c, r, e)
if err == nil {
r.error = e
}
}
}
return
Expand Down
9 changes: 9 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,15 @@ func testError(t *testing.T, c *Client) {
Get("/search")
assertIsError(t, resp, err)
assertEqual(t, 10001, errMsg.ErrorCode)

c.SetCommonError(&errMsg)
resp, err = c.R().
SetQueryParam("username", "").
Get("/search")
assertIsError(t, resp, err)
em, ok := resp.Error().(*ErrorMessage)
assertEqual(t, true, ok)
assertEqual(t, 10000, em.ErrorCode)
}

func TestForm(t *testing.T) {
Expand Down

0 comments on commit f6a7b91

Please sign in to comment.