diff --git a/client.go b/client.go index d33d629..a376256 100644 --- a/client.go +++ b/client.go @@ -980,8 +980,8 @@ func (c *Client) SetCloseConnection(close bool) *Client { // // Note: Response middlewares are not applicable, if you use this option. Basically you have // taken over the control of response parsing from `Resty`. -func (c *Client) SetDoNotParseResponse(parse bool) *Client { - c.notParseResponse = parse +func (c *Client) SetDoNotParseResponse(notParse bool) *Client { + c.notParseResponse = notParse return c } @@ -1234,8 +1234,12 @@ func (c *Client) execute(req *Request) (*Response, error) { } if err != nil || req.notParseResponse || c.notParseResponse { + logErr := responseLogger(c, response) response.setReceivedAt() - return response, err + if err != nil { + return response, errors.Join(err, logErr) + } + return response, wrapNoRetryErr(logErr) } if !req.isSaveResponse { @@ -1247,6 +1251,7 @@ func (c *Client) execute(req *Request) (*Response, error) { if _, ok := body.(*gzip.Reader); !ok { body, err = gzip.NewReader(body) if err != nil { + err = errors.Join(err, responseLogger(c, response)) response.setReceivedAt() return response, err } @@ -1255,6 +1260,7 @@ func (c *Client) execute(req *Request) (*Response, error) { } if response.body, err = readAllWithLimit(body, req.responseBodyLimit); err != nil { + err = errors.Join(err, responseLogger(c, response)) response.setReceivedAt() return response, err } @@ -1265,6 +1271,11 @@ func (c *Client) execute(req *Request) (*Response, error) { response.setReceivedAt() // after we read the body // Apply Response middleware + err = responseLogger(c, response) + if err != nil { + return response, wrapNoRetryErr(err) + } + for _, f := range c.afterResponse { if err = f(c, response); err != nil { break @@ -1460,7 +1471,6 @@ func createClient(hc *http.Client) *Client { // default after response middlewares c.afterResponse = []ResponseMiddleware{ - responseLogger, parseResponseBody, saveResponseIntoFile, } diff --git a/client_test.go b/client_test.go index acd31d4..748be25 100644 --- a/client_test.go +++ b/client_test.go @@ -1111,7 +1111,7 @@ func TestResponseBodyLimit(t *testing.T) { _, err := c.R().Get(ts.URL + "/") assertNotNil(t, err) - assertEqual(t, err, ErrResponseBodyTooLarge) + assertErrorIs(t, ErrResponseBodyTooLarge, err) }) t.Run("request body limit", func(t *testing.T) { @@ -1119,7 +1119,7 @@ func TestResponseBodyLimit(t *testing.T) { _, err := c.R().SetResponseBodyLimit(1024).Get(ts.URL + "/") assertNotNil(t, err) - assertEqual(t, err, ErrResponseBodyTooLarge) + assertErrorIs(t, ErrResponseBodyTooLarge, err) }) t.Run("body less than limit", func(t *testing.T) { @@ -1149,6 +1149,6 @@ func TestResponseBodyLimit(t *testing.T) { c := dc() _, err := c.R().SetResponseBodyLimit(10240).Get(tse.URL + "/") - assertErrorIs(t, err, gzip.ErrHeader) + assertErrorIs(t, gzip.ErrHeader, err) }) } diff --git a/context_test.go b/context_test.go index 5d60cff..1bbc83d 100644 --- a/context_test.go +++ b/context_test.go @@ -8,8 +8,8 @@ package resty import ( "context" + "errors" "net/http" - "net/url" "strings" "sync/atomic" "testing" @@ -208,9 +208,5 @@ func TestRequestContext(t *testing.T) { } func errIsContextCanceled(err error) bool { - ue, ok := err.(*url.Error) - if !ok { - return false - } - return ue.Err == context.Canceled + return errors.Is(err, context.Canceled) }