Skip to content

Commit

Permalink
xerrors: fix Is panics if target is uncomparable
Browse files Browse the repository at this point in the history
Analogous change to golang.org/cl/175260

Fixes golang/go#31841

Change-Id: I93bf093e8d5c13269fe7164a3a1b6956450998e5
Reviewed-on: https://go-review.googlesource.com/c/xerrors/+/174980
Reviewed-by: Brad Fitzpatrick <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
Run-TryBot: Brad Fitzpatrick <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
  • Loading branch information
cuonglm authored and neild committed May 6, 2019
1 parent 1f06c39 commit 3850056
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ func Is(err, target error) bool {
if target == nil {
return err == target
}

isComparable := target == nil || reflect.TypeOf(target).Comparable()
for {
if err == target {
if isComparable && err == target {
return true
}
if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
Expand Down
19 changes: 19 additions & 0 deletions wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func TestIs(t *testing.T) {
{poser, errb, false},
{poser, erro, false},
{poser, errco, false},
{errorUncomparable{}, errorUncomparable{}, true},
{errorUncomparable{}, &errorUncomparable{}, false},
{&errorUncomparable{}, errorUncomparable{}, true},
{&errorUncomparable{}, &errorUncomparable{}, false},
{errorUncomparable{}, err1, false},
{&errorUncomparable{}, err1, false},
}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
Expand Down Expand Up @@ -237,3 +243,16 @@ type errWrap struct{ error }
func (errWrap) Error() string { return "wrapped" }

func (errWrap) Unwrap() error { return nil }

type errorUncomparable struct {
f []string
}

func (errorUncomparable) Error() string {
return "uncomparable error"
}

func (errorUncomparable) Is(target error) bool {
_, ok := target.(errorUncomparable)
return ok
}

0 comments on commit 3850056

Please sign in to comment.