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

fix: reslice error log to remove nil values #81

Merged
merged 2 commits into from Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions retry.go
Expand Up @@ -157,7 +157,7 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error {
}
n++
errorLog[n] = config.context.Err()
return errorLog
return errorLog[:lenWithoutNil(errorLog)]
}

} else {
Expand All @@ -175,7 +175,7 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error {
if config.lastErrorOnly {
return errorLog[lastErrIndex]
}
return errorLog
return errorLog[:lenWithoutNil(errorLog)]
}

func newDefaultRetryConfig() *Config {
Expand Down Expand Up @@ -243,7 +243,7 @@ when you need unwrap all erros, you should use `WrappedErrors()` instead
added in version 4.2.0
*/
func (e Error) Unwrap() error {
return e[len(e)-1]
return e[lenWithoutNil(e)-1]
}

func lenWithoutNil(e Error) (count int) {
Expand Down
10 changes: 7 additions & 3 deletions retry_test.go
Expand Up @@ -31,6 +31,7 @@ func TestDoAllFailed(t *testing.T) {
#8: test
#9: test
#10: test`
assert.Len(t, err, 10)
assert.Equal(t, expectedErrorFormat, err.Error(), "retry error format")
assert.Equal(t, uint(45), retrySum, "right count of retry")
}
Expand Down Expand Up @@ -68,6 +69,7 @@ func TestRetryIf(t *testing.T) {
#1: test
#2: test
#3: special`
assert.Len(t, err, 3)
assert.Equal(t, expectedErrorFormat, err.Error(), "retry error format")
assert.Equal(t, uint(2), retryCount, "right count of retry")

Expand Down Expand Up @@ -163,16 +165,17 @@ func TestLastErrorOnly(t *testing.T) {

func TestUnrecoverableError(t *testing.T) {
attempts := 0
expectedErr := errors.New("error")
testErr := errors.New("error")
expectedErr := Error{testErr}
err := Do(
func() error {
attempts++
return Unrecoverable(expectedErr)
return Unrecoverable(testErr)
},
Attempts(2),
LastErrorOnly(true),
)
assert.Equal(t, expectedErr, err)
assert.Equal(t, testErr, errors.Unwrap(err))
assert.Equal(t, 1, attempts, "unrecoverable error broke the loop")
}

Expand Down Expand Up @@ -366,6 +369,7 @@ func TestContext(t *testing.T) {
#1: test
#2: test
#3: context canceled`
assert.Len(t, err, 3)
assert.Equal(t, expectedErrorFormat, err.Error(), "retry error format")
assert.Equal(t, 2, retrySum, "called at most once")
})
Expand Down