Skip to content

Commit

Permalink
feat: Add OnError to Client
Browse files Browse the repository at this point in the history
Support the error hook, which will be executed if any error will be returned
  • Loading branch information
imroc committed Sep 15, 2023
1 parent 3ab7af1 commit 21fe799
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
15 changes: 13 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (
"strings"
"time"

utls "github.com/refraction-networking/utls"
"golang.org/x/net/publicsuffix"

"github.com/imroc/req/v3/http2"
"github.com/imroc/req/v3/internal/header"
"github.com/imroc/req/v3/internal/util"
utls "github.com/refraction-networking/utls"
"golang.org/x/net/publicsuffix"
)

// DefaultClient returns the global default Client.
Expand Down Expand Up @@ -70,8 +71,11 @@ type Client struct {
roundTripWrappers []RoundTripWrapper
responseBodyTransformer func(rawBody []byte, req *Request, resp *Response) (transformedBody []byte, err error)
resultStateCheckFunc func(resp *Response) ResultState
onError ErrorHook
}

type ErrorHook func(client *Client, req *Request, resp *Response, err error)

// R create a new request.
func (c *Client) R() *Request {
return &Request{
Expand Down Expand Up @@ -981,6 +985,13 @@ func (c *Client) SetProxy(proxy func(*http.Request) (*urlpkg.URL, error)) *Clien
return c
}

// OnError set the error hook which will be executed if any error returned,
// even if the occurs before request is sent (e.g. invalid URL).
func (c *Client) OnError(hook ErrorHook) *Client {
c.onError = hook
return c
}

// OnBeforeRequest add a request middleware which hooks before request sent.
func (c *Client) OnBeforeRequest(m RequestMiddleware) *Client {
c.udBeforeRequest = append(c.udBeforeRequest, m)
Expand Down
14 changes: 9 additions & 5 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/hashicorp/go-multierror"

"github.com/imroc/req/v3/internal/dump"
"github.com/imroc/req/v3/internal/header"
"github.com/imroc/req/v3/internal/util"
Expand Down Expand Up @@ -285,9 +286,11 @@ func (r *Request) SetFile(paramName, filePath string) *Request {
})
}

var errMissingParamName = errors.New("missing param name in multipart file upload")
var errMissingFileName = errors.New("missing filename in multipart file upload")
var errMissingFileContent = errors.New("missing file content in multipart file upload")
var (
errMissingParamName = errors.New("missing param name in multipart file upload")
errMissingFileName = errors.New("missing filename in multipart file upload")
errMissingFileContent = errors.New("missing file content in multipart file upload")
)

// SetFileUpload set the fully custimized multipart file upload options.
func (r *Request) SetFileUpload(uploads ...FileUpload) *Request {
Expand Down Expand Up @@ -695,8 +698,6 @@ func (r *Request) do() (resp *Response, err error) {
resp.result = nil
resp.error = nil
}

return
}

// Send fires http request with specified method and url, returns the
Expand All @@ -705,6 +706,9 @@ func (r *Request) Send(method, url string) (*Response, error) {
r.Method = method
r.RawURL = url
resp := r.Do()
if resp.Err != nil && r.client.onError != nil {
r.client.onError(r.client, r, resp, resp.Err)
}
return resp, resp.Err
}

Expand Down

0 comments on commit 21fe799

Please sign in to comment.