/
errors.go
97 lines (81 loc) · 1.64 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
package api
import (
"fmt"
"github.com/pkg/errors"
)
type Error interface {
Error() string
Code() int
Message() string
Cause() error
}
type baseError struct {
Err error
Msg string
Code int
}
func (x *baseError) Message() string {
if x.Msg != "" {
return x.Msg
}
return x.Err.Error()
}
func (x *baseError) Cause() error {
return x.Err
}
type userError struct{ baseError }
func (x *userError) Error() string { return "UserError: " + x.Msg + "\n" + x.Err.Error() }
func (x *userError) Code() int {
if x.baseError.Code > 0 {
return x.baseError.Code
}
return 400
}
func wrapUserError(err error, code int, msg string) Error {
return &userError{
baseError: baseError{
Err: errors.Wrap(err, msg),
Code: code,
},
}
}
func newUserErrorf(code int, msg string, args ...interface{}) Error {
return &userError{
baseError: baseError{
Msg: fmt.Sprintf(msg, args...),
Code: code,
},
}
}
type systemError struct{ baseError }
func (x *systemError) Error() string { return "SystemError: " + x.Msg + "\n" + x.Err.Error() }
func (x *systemError) Code() int {
if x.baseError.Code > 0 {
return x.baseError.Code
}
return 500
}
func wrapSystemError(err error, code int, msg string) Error {
return &systemError{
baseError: baseError{
Err: errors.Wrap(err, msg),
Code: code,
},
}
}
func wrapSystemErrorf(err error, code int, msg string, args ...interface{}) Error {
return &systemError{
baseError: baseError{
Err: errors.Wrap(err, fmt.Sprintf(msg, args...)),
Code: code,
},
}
}
func newSystemError(msg string, code int) Error {
return &systemError{
baseError: baseError{
Msg: msg,
Code: code,
},
}
}