Skip to content

Commit

Permalink
fix: do not use errors.Join, just use custom error approach (#863)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm authored Sep 21, 2024
1 parent 5d33e1a commit ce7695b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
12 changes: 7 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1243,12 +1243,14 @@ func (c *Client) execute(req *Request) (*Response, error) {
}

if err != nil || req.notParseResponse || c.notParseResponse {
logErr := responseLogger(c, response)
response.setReceivedAt()
if logErr := responseLogger(c, response); logErr != nil {
return response, wrapErrors(logErr, err)
}
if err != nil {
return response, errors.Join(err, logErr)
return response, err
}
return response, wrapNoRetryErr(logErr)
return response, nil
}

if !req.isSaveResponse {
Expand All @@ -1260,7 +1262,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))
err = wrapErrors(responseLogger(c, response), err)
response.setReceivedAt()
return response, err
}
Expand All @@ -1269,7 +1271,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))
err = wrapErrors(responseLogger(c, response), err)
response.setReceivedAt()
return response, err
}
Expand Down
26 changes: 26 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,32 @@ func copyHeaders(hdrs http.Header) http.Header {
return nh
}

func wrapErrors(n error, inner error) error {
if inner == nil {
return n
}
if n == nil {
return inner
}
return &restyError{
err: n,
inner: inner,
}
}

type restyError struct {
err error
inner error
}

func (e *restyError) Error() string {
return e.err.Error()
}

func (e *restyError) Unwrap() error {
return e.inner
}

type noRetryErr struct {
err error
}
Expand Down
16 changes: 16 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package resty

import (
"bytes"
"errors"
"mime/multipart"
"testing"
)
Expand Down Expand Up @@ -89,3 +90,18 @@ func TestWriteMultipartFormFileReaderError(t *testing.T) {
assertNotNil(t, err)
assertEqual(t, "read error", err.Error())
}

func TestRestyErrorFuncs(t *testing.T) {
ne1 := errors.New("new error 1")
nie1 := errors.New("inner error 1")

e := wrapErrors(ne1, nie1)
assertEqual(t, "new error 1", e.Error())
assertEqual(t, "inner error 1", errors.Unwrap(e).Error())

e = wrapErrors(ne1, nil)
assertEqual(t, "new error 1", e.Error())

e = wrapErrors(nil, nie1)
assertEqual(t, "inner error 1", e.Error())
}

0 comments on commit ce7695b

Please sign in to comment.