Skip to content

Commit

Permalink
fix IsPermanent and add test
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Elliott <number101010@gmail.com>
  • Loading branch information
joe-elliott committed Feb 13, 2021
1 parent 5cc0707 commit 8489c06
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 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
}
6 changes: 5 additions & 1 deletion 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,9 +28,12 @@ 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) {
var err error
require.False(t, IsPermanent(err))
}
}

0 comments on commit 8489c06

Please sign in to comment.