-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
66 lines (56 loc) · 1.13 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
package errors
type BizError struct {
msg string
Code int
}
const (
NotFoundErrorCode = 404
InvalidFormErrorCode = 400
ForbiddenErrorCode = 403
)
func (f *BizError) Error() string {
return f.msg
}
func IsBizError(err error) bool {
_, ok := err.(*BizError)
return ok
}
func NotFound(msg string) error {
return &BizError{
Code: NotFoundErrorCode,
msg: msg,
}
}
func IsNotFound(err error) bool {
bizError, ok := err.(*BizError)
if !ok {
return false
}
return bizError.Code == NotFoundErrorCode
}
func InvalidForm(msg string) error {
return &BizError{
Code: InvalidFormErrorCode,
msg: msg,
}
}
func IsInvalidForm(err error) bool {
bizError, ok := err.(*BizError)
if !ok {
return false
}
return bizError.Code == InvalidFormErrorCode
}
func Forbidden(msg string) error {
return &BizError{
Code: ForbiddenErrorCode,
msg: msg,
}
}
func IsForbidden(err error) bool {
bizError, ok := err.(*BizError)
if !ok {
return false
}
return bizError.Code == ForbiddenErrorCode
}