-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
113 lines (87 loc) · 2.02 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package service
import "github.com/pkg/errors"
type existErr interface {
Exist() bool
}
type modelExistErr struct {
msg string
}
func (err *modelExistErr) Error() string {
return err.msg
}
func (err *modelExistErr) Exist() bool {
return true
}
// IsModelExistErr returns true if model already exists
func IsModelExistErr(err error) bool {
ne, ok := errors.Cause(err).(existErr)
return ok && ne.Exist()
}
type notExistErr interface {
NotExist() bool
}
type modelNotExistErr struct {
msg string
}
func (err *modelNotExistErr) Error() string {
return err.msg
}
func (err *modelNotExistErr) NotExist() bool {
return true
}
// IsModelNotExistErr returns true if model does not exist
func IsModelNotExistErr(err error) bool {
ne, ok := errors.Cause(err).(notExistErr)
return ok && ne.NotExist()
}
type validationErr interface {
Valid() bool
}
type modelValidationErr struct {
msg string
}
func (err *modelValidationErr) Error() string {
return err.msg
}
func (err *modelValidationErr) Valid() bool {
return true
}
// IsModelValidationErr returns true if model is invalid
func IsModelValidationErr(err error) bool {
ve, ok := errors.Cause(err).(validationErr)
return ok && ve.Valid()
}
type serviceErr interface {
Service() bool
}
type externalServiceErr struct {
msg string
}
func (err *externalServiceErr) Error() string {
return err.msg
}
func (err *externalServiceErr) Service() bool {
return true
}
// IsExternalServiceErr returns true if external service raises an error
func IsExternalServiceErr(err error) bool {
es, ok := errors.Cause(err).(serviceErr)
return ok && es.Service()
}
type invalidArgErr interface {
InvalidArgument() bool
}
type invalidArgumentErr struct {
msg string
}
func (err *invalidArgumentErr) Error() string {
return err.msg
}
func (err *invalidArgumentErr) InvalidArgument() bool {
return true
}
// IsInvalidArgumentErr returns true if a given argument is invalid
func IsInvalidArgumentErr(err error) bool {
ia, ok := errors.Cause(err).(invalidArgErr)
return ok && ia.InvalidArgument()
}