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 IsPermanent to account for wrapped errors #2455

Merged
merged 2 commits into from
Feb 13, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions consumer/consumererror/permanenterror.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
// error type/instance.
package consumererror

import "errors"

// permanent is an error that will be always returned if its source
// receives the same inputs.
type permanent struct {
err error
}

// permanentError exists to test errors for "IsPermanent"
var permanentError = &permanent{}

// Permanent wraps an error to indicate that it is a permanent error, i.e.: an
// error that will be always returned if its source receives the same inputs.
func Permanent(err error) error {
Expand All @@ -38,8 +43,7 @@ func (p permanent) Error() string {
// that its sources receives the same input.
func IsPermanent(err error) bool {
if err != nil {
_, isPermanent := err.(permanent)
return isPermanent
return errors.As(err, permanentError)
}
return false
}
4 changes: 4 additions & 0 deletions consumer/consumererror/permanenterror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package consumererror

import (
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -27,6 +28,9 @@ func TestPermanent(t *testing.T) {

err = Permanent(err)
require.True(t, IsPermanent(err))

err = fmt.Errorf("%w", err)
require.True(t, IsPermanent(err))
}

func TestIsPermanent_NilError(t *testing.T) {
Expand Down