-
Notifications
You must be signed in to change notification settings - Fork 70
/
error.go
52 lines (46 loc) · 1.1 KB
/
error.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
package util
import (
"github.com/go-sql-driver/mysql"
"github.com/juju/errors"
"github.com/ngaut/log"
"github.com/pingcap/parser/terror"
)
// IsErrDupEntry returns true if error code = 1062
func IsErrDupEntry(err error) bool {
return isMySQLError(err, 1062)
}
// IsErrTableNotExists checks whether err is TableNotExists error
func IsErrTableNotExists(err error) bool {
return isMySQLError(err, 1146)
}
func isMySQLError(err error, code uint16) bool {
err = originError(err)
e, ok := err.(*mysql.MySQLError)
return ok && e.Number == code
}
// originError return original error
func originError(err error) error {
for {
e := errors.Cause(err)
if e == err {
break
}
err = e
}
return err
}
// IgnoreErrors returs true if ignoreErrs contains err
func IgnoreErrors(err error, ignoreErrs []terror.ErrCode) bool {
mysqlErr, ok := originError(err).(*mysql.MySQLError)
if !ok {
log.Errorf("[error: %v] is not mysql error", err)
return false
}
errCode := terror.ErrCode(mysqlErr.Number)
for _, ignoreErrC := range ignoreErrs {
if errCode == ignoreErrC {
return true
}
}
return false
}