Skip to content

Commit

Permalink
fix: request/response logging for SetDoNotParseResponse(true) (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
kon3gor authored Sep 2, 2024
1 parent c22b968 commit 08e6170
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
18 changes: 14 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -1460,7 +1471,6 @@ func createClient(hc *http.Client) *Client {

// default after response middlewares
c.afterResponse = []ResponseMiddleware{
responseLogger,
parseResponseBody,
saveResponseIntoFile,
}
Expand Down
6 changes: 3 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1111,15 +1111,15 @@ 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) {
c := dc()

_, 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) {
Expand Down Expand Up @@ -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)
})
}
8 changes: 2 additions & 6 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ package resty

import (
"context"
"errors"
"net/http"
"net/url"
"strings"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -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)
}

0 comments on commit 08e6170

Please sign in to comment.