-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.go
141 lines (113 loc) · 2.3 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
package echotool
import (
"fmt"
"sync"
)
type Kind int
const (
KindHeader Kind = iota
KindParam
KindQuery
KindPostForm
KindForm
KindEnv
KindCookie
)
var _ fmt.Stringer = (*Kind)(nil)
func (k Kind) String() string {
switch k {
case KindHeader:
return "header"
case KindParam:
return "param"
case KindQuery:
return "query"
case KindPostForm:
return "postform"
case KindForm:
return "form"
case KindEnv:
return "env"
case KindCookie:
return "cookie"
}
return fmt.Sprintf("unknown kind: %d", k)
}
type EmptyError struct {
kind Kind
key string
}
var _ error = (*EmptyError)(nil)
func NewEmptyError(kind Kind, key string) *EmptyError {
return &EmptyError{
kind: kind,
key: key,
}
}
func NewHeaderEmptyError(key string) *EmptyError {
return NewEmptyError(KindHeader, key)
}
func NewParamEmptyError(key string) *EmptyError {
return NewEmptyError(KindParam, key)
}
func NewQueryEmptyError(key string) *EmptyError {
return NewEmptyError(KindQuery, key)
}
func NewPostFormEmptyError(key string) *EmptyError {
return NewEmptyError(KindPostForm, key)
}
func NewFormEmptyError(key string) *EmptyError {
return NewEmptyError(KindForm, key)
}
func NewEnvEmptyError(key string) *EmptyError {
return NewEmptyError(KindEnv, key)
}
func NewCookieEmptyError(key string) *EmptyError {
return NewEmptyError(KindCookie, key)
}
func (e EmptyError) Error() string {
return fmt.Sprintf("%s(%s) is empty", e.key, e.kind.String())
}
func IsEmptyError(err error) bool {
_, ok := err.(*EmptyError)
return ok
}
type EchotoolError struct {
code int
err error
}
var _ error = (*EchotoolError)(nil)
func (e EchotoolError) GetCode() int {
return e.code
}
func (e EchotoolError) GetError() error {
return e.err
}
func (e EchotoolError) Error() string {
return fmt.Sprintf("%s - %+v", CodeMsg(e.code), e.err)
}
func (e *EchotoolError) reset() {
e.code = 0
e.err = nil
}
var echotoolErrorPool sync.Pool
func AcquireEchotoolError(code int, err error) (e *EchotoolError) {
if v := echotoolErrorPool.Get(); v != nil {
e = v.(*EchotoolError)
} else {
e = &EchotoolError{}
}
e.code = code
e.err = err
return
}
func ReleaseEchotoolError(e *EchotoolError) {
if e != nil {
e.reset()
echotoolErrorPool.Put(e)
}
}
func IsEchotoolError(err error) bool {
_, ok := err.(*EchotoolError)
return ok
}