Skip to content

Commit

Permalink
Merge pull request #79 from JamieEdge/fix-78
Browse files Browse the repository at this point in the history
fix: return last non-nil value when unwrapping errors
  • Loading branch information
JaSei committed Jan 3, 2023
2 parents b30adbd + 54c42e0 commit a297da9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion retry.go
Expand Up @@ -243,7 +243,7 @@ When you need to unwrap all errors, 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
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

0 comments on commit a297da9

Please sign in to comment.