Skip to content

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

@JamieEdge

Description

@JamieEdge

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions