-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
260 lines (244 loc) · 4.86 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Package errors provides error wrapping with stack trace
package errors
import (
stderr "errors"
"reflect"
"strings"
)
// Op is an operation during which an error has occurred.
type Op string
// Error is an error wrapper.
type Error struct {
Op Op
Kind ErrorKind
Code ErrorCode
Msg string
Cause error
Stack StackTrace
}
// E creates or wraps an error.
// Arguments could be an Op, ErrorKind, ErrorCode, string message, or an error to wrap.
func E(args ...interface{}) *Error {
e := &Error{}
wrapping := false
for _, a := range args {
switch a := a.(type) {
case Op:
if e.Op != "" {
panic("bad call to E: multiple ops")
}
e.Op = a
case ErrorKind:
e.Kind |= a
case ErrorCode:
if e.Code != 0 {
panic("bad call to E: multiple codes")
}
e.Code = a
case string:
if e.Msg != "" {
panic("bad call to E: multiple messages")
}
e.Msg = a
case error:
if e.Cause != nil {
panic("bad call to E: multiple causes")
}
e.Cause = a
wrapping = true
case nil:
return nil
default:
panic("bad call to E: argument of type " + reflect.TypeOf(a).String())
}
}
shouldTrace := true
if wrapping {
if _, ok := e.Cause.(*Error); ok {
shouldTrace = false
}
}
if shouldTrace {
e.Stack = callers()
}
return e
}
// Unwrap returns the result of calling the Unwrap method on err, if err's
// type contains an Unwrap method returning error.
// Otherwise, Unwrap returns nil.
func Unwrap(err error) error {
u, ok := err.(interface {
Unwrap() error
})
if !ok {
return nil
}
return u.Unwrap()
}
// Ops returns stack of error operations.
func Ops(err error) []Op {
e, ok := err.(*Error)
if !ok {
return []Op{}
}
var res []Op
if e.Op != "" {
res = append(res, e.Op)
}
cause, ok := e.Cause.(*Error)
if !ok {
return res
}
res = append(res, Ops(cause)...)
return res
}
// Trace returns error's stack trace.
func Trace(err error) StackTrace {
e, ok := err.(*Error)
if !ok {
return nil
}
cause, ok := e.Cause.(*Error)
if !ok {
return e.Stack
}
return Trace(cause)
}
// Kind returns error's kind.
func Kind(err error) ErrorKind {
e, ok := err.(*Error)
if !ok {
return 0
}
if e.Kind != 0 {
return e.Kind
}
if e.Cause != nil {
return Kind(e.Cause)
}
return 0
}
// Code returns error's code.
func Code(err error) ErrorCode {
e, ok := err.(*Error)
if !ok {
return Unexpected
}
if e.Code != 0 {
return e.Code
}
if e.Cause != nil {
return Code(e.Cause)
}
return Unexpected
}
// Is checks if err is of given kind, has given code or matches given error what.
func Is(err error, what interface{}) bool {
if err == nil {
return false
}
if err, ok := err.(*Error); ok {
return err.Is(what)
}
if list, ok := err.(List); ok {
for i := range list {
if Is(list[i], what) {
return true
}
}
return false
}
if _, ok := what.(ErrorKind); ok {
return false
}
if code, ok := what.(ErrorCode); ok {
return code == Unexpected
}
if target, ok := what.(error); ok {
return stderr.Is(err, target)
}
panic("what must be ErrorKind, ErrorCode or error")
}
// IsAnyOf checks if err is any of the given kinds or has any of the given codes
func IsAnyOf(err error, what ...interface{}) bool {
if err == nil {
return false
}
for i := range what {
if Is(err, what[i]) {
return true
}
}
return false
}
// As finds the first error in err's chain that matches target, and if so, sets
// target to that error value and returns true. Otherwise, it returns false.
// If err is an error list, it does so for every error in the list.
func As(err error, target interface{}) bool {
if err == nil {
return false
}
if list, ok := err.(List); ok {
for i := range list {
if As(list[i], target) {
return true
}
}
}
return stderr.As(err, target)
}
// ClientMsg returns error message suitable to display to the client.
func ClientMsg(err error) string {
e, ok := err.(*Error)
if !ok {
return ""
}
if e.Kind&Client > 0 {
return e.Msg
}
if e.Cause != nil {
return ClientMsg(e.Cause)
}
return ""
}
// Error return human readable representation of an error.
func (e *Error) Error() string {
sb := &strings.Builder{}
if e.Msg != "" {
sb.WriteString(e.Msg)
}
if e.Cause != nil {
causeMsg := e.Cause.Error()
if e.Msg != "" && causeMsg != "" {
sb.WriteString(": ")
}
sb.WriteString(causeMsg)
}
return sb.String()
}
// Unwrap unwraps an error
func (e *Error) Unwrap() error {
return e.Cause
}
// Is checks if e is of given kind, has given code or matches given error what.
func (e *Error) Is(what interface{}) bool {
if what, ok := what.(error); ok {
return stderr.Is(e, what)
}
switch what := what.(type) {
case ErrorKind:
if e.Kind != 0 {
return e.Kind&what > 0
}
case ErrorCode:
if e.Code != 0 {
return e.Code == what
}
default:
panic("what must be ErrorKind, ErrorCode or error")
}
if e.Cause != nil {
return Is(e.Cause, what)
}
return false
}