-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
85 lines (68 loc) · 1.48 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
package ep
import "errors"
type Op string
type ErrorKind uint8
const (
OtherError ErrorKind = iota
ServerError // unexpected server condition
UnacceptableError // no encoder supports what the client accepts
UnsupportedError // no decoder supports the content type sent by the client
RequestHookError // request hook failed to run
DecoderError // decoder failed while decoding
EncoderError // encoder failed while encoding
)
type Error struct {
msg string
err error
op Op
kind ErrorKind
}
func (e *Error) Unwrap() error {
return e.err
}
func (e *Error) Is(target error) bool {
terr, ok := target.(*Error)
if !ok {
return false
}
if terr.op != "" && terr.op != e.op {
return false
}
if terr.kind != OtherError && terr.kind != e.kind {
return false
}
if terr.msg != "" && terr.msg != e.msg {
return false
}
if terr.err != nil {
return errors.Is(e.err, terr.err)
}
return true
}
func (e *Error) Error() string {
if e.err == nil {
return e.msg
}
if e.op == "" {
return e.msg + ": " + e.err.Error()
}
return string(e.op) + ": " + e.msg + ": " + e.err.Error()
}
func Err(args ...interface{}) *Error {
e := &Error{}
for _, arg := range args {
switch at := arg.(type) {
case Op:
e.op = at
case error:
e.err = at
case string:
e.msg = at
case ErrorKind:
e.kind = at
default:
panic("ep: unsupported argument for building error")
}
}
return e
}