-
Notifications
You must be signed in to change notification settings - Fork 39
/
errors.go
353 lines (298 loc) · 6.29 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package errors
import (
"fmt"
"github.com/pkg/errors"
)
type Code int
const CodeSegment = 1000
const (
CodeSCORE Code = iota * CodeSegment
CodeGeneral
CodeService
CodeConsensus
CodeNetwork
CodeBlock
CodeServer
CodeCritical
)
const (
Success Code = 0
UnknownError Code = CodeGeneral + iota
IllegalArgumentError
UnsupportedError
InvalidStateError
NotFoundError
InvalidNetworkError
TimeoutError
ExecutionFailError
InterruptedError
)
var (
ErrUnknown = NewBase(UnknownError, "UnknownError")
ErrIllegalArgument = NewBase(IllegalArgumentError, "IllegalArgument")
ErrInvalidState = NewBase(InvalidStateError, "InvalidState")
ErrUnsupported = NewBase(UnsupportedError, "Unsupported")
ErrNotFound = NewBase(NotFoundError, "NotFound")
ErrInvalidNetwork = NewBase(InvalidNetworkError, "InvalidNetwork")
ErrTimeout = NewBase(TimeoutError, "Timeout")
ErrExecutionFail = NewBase(ExecutionFailError, "ExecutionFail")
ErrInterrupted = NewBase(InterruptedError, "Interrupted")
)
const (
CriticalUnknownError = CodeCritical + iota
CriticalIOError
CriticalFormatError
CriticalHashError
)
func IsCriticalCode(c Code) bool {
return c >= CodeCritical && c < CodeCritical+CodeSegment
}
func IsCritical(e error) bool {
return IsCriticalCode(CodeOf(e))
}
func (c Code) New(msg string) error {
return Errorc(c, msg)
}
func (c Code) Errorf(f string, args ...interface{}) error {
return Errorcf(c, f, args...)
}
func (c Code) Wrap(e error, msg string) error {
return Wrapc(e, c, msg)
}
func (c Code) Wrapf(e error, f string, args ...interface{}) error {
return Wrapcf(e, c, f, args...)
}
func (c Code) AttachTo(e error) error {
return WithCode(e, c)
}
func (c Code) Equals(e error) bool {
if e == nil {
return false
}
return CodeOf(e) == c
}
/*------------------------------------------------------------------------------
Simple mapping to github.com/pkg/errors for easy stack print
*/
// New makes an error including a stack without any code
// If you want to make base error without stack
func New(msg string) error {
return errors.New(msg)
}
func Errorf(f string, args ...interface{}) error {
return errors.Errorf(f, args...)
}
func WithStack(e error) error {
return errors.WithStack(e)
}
/*------------------------------------------------------------------------------
Base error only with message and code
For general usage, you may return this directly.
*/
type baseError struct {
code Code
msg string
}
func (e *baseError) Error() string {
return e.msg
}
func (e *baseError) ErrorCode() Code {
return e.code
}
func (e *baseError) Format(f fmt.State, c rune) {
switch c {
case 'v', 's', 'q':
fmt.Fprintf(f, "E%04d:%s", e.code, e.msg)
}
}
func (e *baseError) Equals(err error) bool {
return CodeOf(err) == e.code
}
func NewBase(code Code, msg string) *baseError {
return &baseError{code, msg}
}
/*------------------------------------------------------------------------------
Coded error object
*/
type codedError struct {
code Code
error
}
func (e *codedError) Format(f fmt.State, c rune) {
switch c {
case 'v':
if f.Flag('+') {
fmt.Fprintf(f, "E%04d:%+v", e.code, e.error)
return
}
fallthrough
case 's', 'q':
fmt.Fprintf(f, "E%04d:%s", e.code, e.Error())
}
}
func (e *codedError) ErrorCode() Code {
return e.code
}
func (e *codedError) Unwrap() error {
return e.error
}
func Errorc(code Code, msg string) error {
return &codedError{
code: code,
error: errors.New(msg),
}
}
func Errorcf(code Code, f string, args ...interface{}) error {
return &codedError{
code: code,
error: errors.Errorf(f, args...),
}
}
func WithCode(err error, code Code) error {
if err == nil {
return nil
}
if _, ok := CoderOf(err); ok {
return Wrapc(err, code, err.Error())
}
return &codedError{
code: code,
error: err,
}
}
type messageError struct {
error
origin error
}
func (e *messageError) Format(f fmt.State, c rune) {
switch c {
case 'v':
if f.Flag('+') {
fmt.Fprintf(f, "%+v", e.error)
fmt.Fprintf(f, "\nWrapping %+v", e.origin)
return
}
fallthrough
case 's', 'q':
fmt.Fprintf(f, "%s", e.error)
}
}
func (e *messageError) Unwrap() error {
return e.origin
}
func Wrap(e error, msg string) error {
return &messageError{
error: errors.New(msg),
origin: e,
}
}
func Wrapf(e error, f string, args ...interface{}) error {
return &messageError{
error: errors.Errorf(f, args...),
origin: e,
}
}
type wrappedError struct {
error
code Code
origin error
}
func (e *wrappedError) Format(f fmt.State, c rune) {
switch c {
case 'v':
if f.Flag('+') {
fmt.Fprintf(f, "E%04d:%+v", e.code, e.error)
fmt.Fprintf(f, "\nWrapping %+v", e.origin)
return
}
fallthrough
case 'q', 's':
fmt.Fprintf(f, "E%04d:%s", e.code, e.error)
}
}
func (e *wrappedError) Unwrap() error {
return e.origin
}
func (e *wrappedError) ErrorCode() Code {
return e.code
}
func Wrapc(e error, c Code, msg string) error {
return &wrappedError{
error: errors.New(msg),
code: c,
origin: e,
}
}
func Wrapcf(e error, c Code, f string, args ...interface{}) error {
return &wrappedError{
error: errors.Errorf(f, args...),
code: c,
origin: e,
}
}
type ErrorCoder interface {
error
ErrorCode() Code
}
func CoderOf(e error) (ErrorCoder, bool) {
coder := FindCause(e, func(err error) bool {
_, ok := err.(ErrorCoder)
return ok
})
if coder != nil {
return coder.(ErrorCoder), true
} else {
return nil, false
}
}
func CodeOf(e error) Code {
if e == nil {
return Success
}
if coder, ok := CoderOf(e); ok {
return coder.ErrorCode()
}
return UnknownError
}
// Unwrapper is interface to unwrap the error to get the origin error.
type Unwrapper interface {
Unwrap() error
}
// Is checks whether err is caused by the target.
func Is(err, target error) bool {
cause := FindCause(err, func(err error) bool {
return err == target
})
return cause != nil
}
func FindCause(err error, cb func(err error) bool) error {
type causer interface {
Cause() error
}
type unwrapper interface {
Unwrap() error
}
for {
if err == nil {
return nil
}
if cb(err) {
return err
}
switch obj := err.(type) {
case causer:
err = obj.Cause()
case unwrapper:
err = obj.Unwrap()
default:
return nil
}
}
}
func ToString(e error) string {
if e == nil {
return ""
} else {
return fmt.Sprintf("%v", e)
}
}