Skip to content

Commit 54c42e0

Browse files
committed
fix: return last non-nil value when unwrapping errors
Refs: avast#78
1 parent affbf8f commit 54c42e0

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

retry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ when you need unwrap all erros, you should use `WrappedErrors()` instead
243243
added in version 4.2.0
244244
*/
245245
func (e Error) Unwrap() error {
246-
return e[len(e)-1]
246+
return e[lenWithoutNil(e)-1]
247247
}
248248

249249
func lenWithoutNil(e Error) (count int) {

retry_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,17 @@ func TestLastErrorOnly(t *testing.T) {
163163

164164
func TestUnrecoverableError(t *testing.T) {
165165
attempts := 0
166-
expectedErr := errors.New("error")
166+
testErr := errors.New("error")
167+
expectedErr := Error{testErr, nil}
167168
err := Do(
168169
func() error {
169170
attempts++
170-
return Unrecoverable(expectedErr)
171+
return Unrecoverable(testErr)
171172
},
172173
Attempts(2),
173-
LastErrorOnly(true),
174174
)
175175
assert.Equal(t, expectedErr, err)
176+
assert.Equal(t, testErr, errors.Unwrap(err))
176177
assert.Equal(t, 1, attempts, "unrecoverable error broke the loop")
177178
}
178179

0 commit comments

Comments
 (0)