-
Notifications
You must be signed in to change notification settings - Fork 39
/
errors.go
55 lines (48 loc) · 1.04 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package errors
import (
"reflect"
gomysql "github.com/siddontang/go-mysql/mysql"
)
// copied from errors.Is(), but replace Unwrap() with Cause()
func Is(err, target error) bool {
if target == nil {
return err == target
}
isComparable := reflect.TypeOf(target).Comparable()
for {
if isComparable && err == target {
return true
}
if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
return true
}
// TODO: consider supporing target.Is(err). This would allow
// user-definable predicates, but also may allow for coping with sloppy
// APIs, thereby making it easier to get away with them.
if err = Cause(err); err == nil {
return false
}
}
}
func CheckAndGetMyError(err error) (*gomysql.MyError, bool) {
if err == nil {
return nil, false
}
for {
if err1, ok := err.(*gomysql.MyError); ok {
return err1, true
}
if err = Cause(err); err == nil {
return nil, false
}
}
}
func Cause(err error) error {
u, ok := err.(interface {
Cause() error
})
if !ok {
return nil
}
return u.Cause()
}