Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not use errors.Join, just use custom error approach #863

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1243,12 +1243,14 @@
}

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)

Check warning on line 1248 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L1248

Added line #L1248 was not covered by tests
}
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 @@
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)

Check warning on line 1265 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L1265

Added line #L1265 was not covered by tests
response.setReceivedAt()
return response, err
}
Expand All @@ -1269,7 +1271,7 @@
}

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())
}