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

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

Closed
JamieEdge opened this issue Dec 24, 2022 · 0 comments · Fixed by #79
Closed

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

JamieEdge opened this issue Dec 24, 2022 · 0 comments · Fixed by #79

Comments

@JamieEdge
Copy link
Contributor

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]
}

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant