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

Length of Error returned by Do does not reflect number of attempts #80

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

Length of Error returned by Do does not reflect number of attempts #80

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

Comments

@JamieEdge
Copy link
Contributor

If an unrecoverable error is encountered before the final attempt, the length of the error returned by Do is equal to the maximum number of attempts rather than the actual number. This behaviour is demonstrated by the following program.

package main

import (
	"errors"
	"fmt"

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

func main() {
	i := 0

	err := retry.Do(
		func() error {
			err := fmt.Errorf("error %d", i)

			if i > 1 {
				err = retry.Unrecoverable(err)
			}

			i++
			return err
		},
	)

	fmt.Println(err)

	var e retry.Error
	if errors.As(err, &e) {
		fmt.Println(len(e))
	}
}

The following output is observed when using v4.3.1.

All attempts fail:
#1: error 0
#2: error 1
#3: error 2
10

The following output is expected instead, where the length of the slice is equal to the number of attempts.

All attempts fail:
#1: error 0
#2: error 1
#3: error 2
3
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