Skip to content

Commit

Permalink
Fix IsPermanent to account for wrapped errors (#2455)
Browse files Browse the repository at this point in the history
* fix IsPermanent and add test

Signed-off-by: Joe Elliott <number101010@gmail.com>

* Update consumer/consumererror/permanenterror_test.go

Co-authored-by: Matthew Wear <matthew.wear@gmail.com>

Co-authored-by: Matthew Wear <matthew.wear@gmail.com>
  • Loading branch information
joe-elliott and mwear authored Feb 13, 2021
1 parent 5cc0707 commit 6d4eeb9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
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

0 comments on commit 6d4eeb9

Please sign in to comment.