...
1
2
3
4
5
6
7
8 package errors
9
10 import "reflect"
11
12
13 func Is(err, target error) bool {
14 if target == nil {
15 return err == target
16 }
17
18 isComparable := reflect.TypeOf(target).Comparable()
19 for {
20 if isComparable && err == target {
21 return true
22 }
23 if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
24 return true
25 }
26 if err = unwrap(err); err == nil {
27 return false
28 }
29 }
30 }
31
32 func unwrap(err error) error {
33 u, ok := err.(interface {
34 Unwrap() error
35 })
36 if !ok {
37 return nil
38 }
39 return u.Unwrap()
40 }
41
View as plain text