avast/retry-go

Error.Unwrap unexpectedly returns nil when all attempts are not performed

JamieEdge opened this issue · 0 comments

If an unrecoverable error is encountered before the final attempt, the unwrapped value of the error returned by Do is nil. This behaviour is demonstrated by the following program.

package main

import (
	"errors"
	"fmt"

	"github.com/avast/retry-go/v4"
)

func main() {
	err := retry.Do(
		func() error {
			return retry.Unrecoverable(errors.New("some error"))
		},
	)

	fmt.Println(err)
	fmt.Println(errors.Unwrap(err))
}

The following output is observed when using v4.3.1.

All attempts fail:
#1: some error
<nil>

The following output is expected instead, where the last encountered error is returned.

All attempts fail:
#1: some error
some error

This is caused by Error.Unwrap not checking for nil values.

retry-go/retry.go

Lines 245 to 247 in affbf8f

func (e Error) Unwrap() error {
return e[len(e)-1]
}