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: return last non-nil value when unwrapping errors #79

Merged
merged 1 commit into from
Jan 3, 2023
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
2 changes: 1 addition & 1 deletion retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ when you need unwrap all erros, you should use `WrappedErrors()` instead
added in version 4.2.0
*/
func (e Error) Unwrap() error {
return e[len(e)-1]
return e[lenWithoutNil(e)-1]
}

func lenWithoutNil(e Error) (count int) {
Expand Down
7 changes: 4 additions & 3 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,17 @@ func TestLastErrorOnly(t *testing.T) {

func TestUnrecoverableError(t *testing.T) {
attempts := 0
expectedErr := errors.New("error")
testErr := errors.New("error")
expectedErr := Error{testErr, nil}
err := Do(
func() error {
attempts++
return Unrecoverable(expectedErr)
return Unrecoverable(testErr)
},
Attempts(2),
LastErrorOnly(true),
)
assert.Equal(t, expectedErr, err)
assert.Equal(t, testErr, errors.Unwrap(err))
assert.Equal(t, 1, attempts, "unrecoverable error broke the loop")
}

Expand Down