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

How to get unwrapped error from retry? #65

Closed
ghost opened this issue Aug 22, 2022 · 2 comments
Closed

How to get unwrapped error from retry? #65

ghost opened this issue Aug 22, 2022 · 2 comments

Comments

@ghost
Copy link

ghost commented Aug 22, 2022

In retry func, if I have error, i return errors.Wrapf(someErr, "someWrap text", some wrap values).
And retry func return me this type for error:
image

But I need extract someErr, from returned error.
Usually I do this by this code:

for errors.Unwrap(err) != nil {
		err = errors.Unwrap(err)
}

But with retry this method doesn't work.

Full code:

err = retry.Do(
		func() error {
			defer func() { sentCount++ }()
			responseBody, statusCode, err = h.Request(ctx, args.RequestParams)
			if err != nil {
				return errors.Wrapf(err, "fhttp.RequestWithRetry(args: %s)", utils.GetStructJSON(args)) // if last retry this need to unwrap err
			}

			if statusCode != args.ExpectedStatusCode {
				if statusCode == fasthttp.StatusUnauthorized {
					return ErrStatusUnauthorized
				}
				return errors.Wrapf(
					ErrNotExpectedStatusCode,
					"fhttp.RequestWithRetry, expeced %d, got %d statusCode; requestArgs: %s, responseBody: %s",
					args.ExpectedStatusCode,
					statusCode,
					utils.GetStructJSON(args),
					responseBody,
				)  // if last retry in this part of code need possibility unwrap to ErrNotExpectedStatusCode
			}
			return nil
		},
		retry.Delay(args.RetryDelay*time.Millisecond),
		retry.RetryIf(func(err error) bool {
			if ((errors.Is(err, fasthttp.ErrTimeout) ||
				errors.Is(err, fasthttp.ErrDialTimeout) ||
				errors.Is(err, fasthttp.ErrTLSHandshakeTimeout)) && sentCount >= 2) ||
				errors.Is(err, ErrStatusUnauthorized) {
				return false
			}
			return true
		}),
		retry.Attempts(args.RetryAttempts),
	)
        for errors.Unwrap(err) != nil { // do nothing 
		err = errors.Unwrap(err)
	}
JaSei added a commit that referenced this issue Oct 19, 2022
@JaSei
Copy link
Collaborator

JaSei commented Oct 19, 2022

it should be solved by #70

JaSei added a commit that referenced this issue Oct 19, 2022
feat: unwrap error support (issue #65)
@JaSei
Copy link
Collaborator

JaSei commented Oct 19, 2022

solved in version 4.2.0

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

No branches or pull requests

1 participant