From 6d4eeb99d5446e7d3afcc9e1b60860188c0d39a7 Mon Sep 17 00:00:00 2001 From: Joe Elliott Date: Sat, 13 Feb 2021 14:02:44 -0500 Subject: [PATCH] Fix IsPermanent to account for wrapped errors (#2455) * fix IsPermanent and add test Signed-off-by: Joe Elliott * Update consumer/consumererror/permanenterror_test.go Co-authored-by: Matthew Wear Co-authored-by: Matthew Wear --- consumer/consumererror/permanenterror.go | 8 ++++++-- consumer/consumererror/permanenterror_test.go | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/consumer/consumererror/permanenterror.go b/consumer/consumererror/permanenterror.go index e4d0950596e..935206f1a57 100644 --- a/consumer/consumererror/permanenterror.go +++ b/consumer/consumererror/permanenterror.go @@ -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 { @@ -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 } diff --git a/consumer/consumererror/permanenterror_test.go b/consumer/consumererror/permanenterror_test.go index 8674825fc57..9974623d729 100644 --- a/consumer/consumererror/permanenterror_test.go +++ b/consumer/consumererror/permanenterror_test.go @@ -16,6 +16,7 @@ package consumererror import ( "errors" + "fmt" "testing" "github.com/stretchr/testify/require" @@ -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) {