From 37c4c88c0bb71f6ea678170b2cbaabbe30792b8b Mon Sep 17 00:00:00 2001 From: Niv Keidan Date: Mon, 10 Jun 2024 12:23:50 +0300 Subject: [PATCH] extract context errors with context.Cause and not ctx.Err() to support users using CancelCause --- go.mod | 2 +- retry.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index adf413c..30161b0 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/avast/retry-go/v4 -go 1.18 +go 1.20 require github.com/stretchr/testify v1.9.0 diff --git a/retry.go b/retry.go index f739915..f0a197d 100644 --- a/retry.go +++ b/retry.go @@ -132,7 +132,7 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) ( opt(config) } - if err := config.context.Err(); err != nil { + if err := context.Cause(config.context); err != nil { return emptyT, err } @@ -161,9 +161,9 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) ( case <-config.timer.After(delay(config, n, err)): case <-config.context.Done(): if config.wrapContextErrorWithLastError { - return emptyT, Error{config.context.Err(), lastErr} + return emptyT, Error{context.Cause(config.context), lastErr} } - return emptyT, config.context.Err() + return emptyT, context.Cause(config.context) } } } @@ -207,10 +207,10 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) ( case <-config.timer.After(delay(config, n, err)): case <-config.context.Done(): if config.lastErrorOnly { - return emptyT, config.context.Err() + return emptyT, context.Cause(config.context) } - return emptyT, append(errorLog, config.context.Err()) + return emptyT, append(errorLog, context.Cause(config.context)) } n++