Skip to content

Commit

Permalink
fix: do not use errors.Join, just use custom error approach
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Sep 21, 2024
1 parent 5d33e1a commit 19530bc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
14 changes: 8 additions & 6 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 err != nil {
return response, errors.Join(err, logErr)
logErr := responseLogger(c, response)
if logErr != nil {
return response, wrapErrors(logErr, err)

Check warning on line 1249 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L1249

Added line #L1249 was not covered by tests
} else if err != nil {
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)

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 @@ 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

Check warning on line 362 in util.go

View check run for this annotation

Codecov / codecov/patch

util.go#L362

Added line #L362 was not covered by tests
}
if n == nil {
return inner
}
return &restyError{
err: n,
inner: inner,

Check warning on line 369 in util.go

View check run for this annotation

Codecov / codecov/patch

util.go#L367-L369

Added lines #L367 - L369 were not covered by tests
}
}

type restyError struct {
err error
inner error
}

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

Check warning on line 379 in util.go

View check run for this annotation

Codecov / codecov/patch

util.go#L378-L379

Added lines #L378 - L379 were not covered by tests
}

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

Check warning on line 383 in util.go

View check run for this annotation

Codecov / codecov/patch

util.go#L382-L383

Added lines #L382 - L383 were not covered by tests
}

type noRetryErr struct {
err error
}
Expand Down

0 comments on commit 19530bc

Please sign in to comment.