-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherrors.go
95 lines (79 loc) · 1.51 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package sqlx
import (
"fmt"
)
func NewSqlError(tpe sqlErrType, msg string) *SqlError {
return &SqlError{
Type: tpe,
Msg: msg,
}
}
type SqlError struct {
Type sqlErrType
Msg string
}
func (e *SqlError) Error() string {
return fmt.Sprintf("Sqlx [%s] %s", e.Type, e.Msg)
}
type sqlErrType string
var (
sqlErrTypeNotFound sqlErrType = "NotFound"
sqlErrTypeConflict sqlErrType = "Conflict"
)
var DuplicateEntryErrNumber uint16 = 1062
func DBErr(err error) *dbErr {
return &dbErr{
err: err,
}
}
type dbErr struct {
err error
errDefault error
errNotFound error
errConflict error
}
func (r dbErr) WithNotFound(err error) *dbErr {
r.errNotFound = err
return &r
}
func (r dbErr) WithDefault(err error) *dbErr {
r.errDefault = err
return &r
}
func (r dbErr) WithConflict(err error) *dbErr {
r.errConflict = err
return &r
}
func (r *dbErr) IsNotFound() bool {
if sqlErr, ok := UnwrapAll(r.err).(*SqlError); ok {
return sqlErr.Type == sqlErrTypeNotFound
}
return false
}
func (r *dbErr) IsConflict() bool {
if sqlErr, ok := UnwrapAll(r.err).(*SqlError); ok {
return sqlErr.Type == sqlErrTypeConflict
}
return false
}
func (r *dbErr) Err() error {
if r.err == nil {
return nil
}
if sqlErr, ok := UnwrapAll(r.err).(*SqlError); ok {
switch sqlErr.Type {
case sqlErrTypeNotFound:
if r.errNotFound != nil {
return r.errNotFound
}
case sqlErrTypeConflict:
if r.errConflict != nil {
return r.errConflict
}
}
if r.errDefault != nil {
return r.errDefault
}
}
return r.err
}