From 0835d1aee9dfeb362745e1ab04d0273d2ee9be0e Mon Sep 17 00:00:00 2001 From: Keiichi Hirobe Date: Tue, 15 Nov 2022 18:40:12 +0900 Subject: [PATCH] fix bug that overwrites last error when context is canceled --- retry.go | 1 + retry_test.go | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/retry.go b/retry.go index 1e2ccff..1c4e763 100644 --- a/retry.go +++ b/retry.go @@ -155,6 +155,7 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error { if config.lastErrorOnly { return config.context.Err() } + n++ errorLog[n] = config.context.Err() return errorLog } diff --git a/retry_test.go b/retry_test.go index 55fbca8..4312409 100644 --- a/retry_test.go +++ b/retry_test.go @@ -364,7 +364,8 @@ func TestContext(t *testing.T) { expectedErrorFormat := `All attempts fail: #1: test -#2: context canceled` +#2: test +#3: context canceled` assert.Equal(t, expectedErrorFormat, err.Error(), "retry error format") assert.Equal(t, 2, retrySum, "called at most once") })