-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
323 lines (282 loc) · 7.43 KB
/
error.go
File metadata and controls
323 lines (282 loc) · 7.43 KB
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
package errors
import (
"encoding/json"
"errors"
"fmt"
"github.com/rs/xid"
"github.com/valyala/bytebufferpool"
)
const (
badRequestErrorCode = 400
badRequestErrorName = "***BAD REQUEST***"
unauthorizedErrorCode = 401
unauthorizedErrorName = "***UNAUTHORIZED***"
forbiddenErrorCode = 403
forbiddenErrorName = "***FORBIDDEN***"
notFoundErrorCode = 404
notFoundErrorName = "***NOT FOUND***"
notAcceptableErrorCode = 406
notAcceptableErrorName = "***NOT ACCEPTABLE***"
timeoutErrorCode = 408
timeoutErrorName = "***TIMEOUT***"
tooEarlyCode = 425
tooEarlyName = "***TOO EARLY***"
tooManyRequestsCode = 429
tooManyRequestsName = "***TOO MANY REQUEST***"
serviceErrorCode = 500
serviceErrorName = "***SERVICE EXECUTE FAILED***"
nilErrorMessage = "NIL"
serviceNotImplementedErrorCode = 501
serviceNotImplementedErrorName = "***SERVICE NOT IMPLEMENTED***"
unavailableErrorCode = 503
unavailableErrorName = "***SERVICE UNAVAILABLE***"
warnErrorCode = 555
warnErrorName = "***WARNING***"
)
type CodeError interface {
Id() string
Code() int
Name() string
Message() string
Stacktrace() (fn string, file string, line int)
WithMeta(key string, value string) (err CodeError)
WithCause(cause error) (err CodeError)
Contains(err error) (has bool)
Error() string
Format(state fmt.State, r rune)
String() string
json.Marshaler
}
func Empty() CodeError {
return &CodeErrorImpl{}
}
func BadRequest(message string) CodeError {
return NewWithDepth(badRequestErrorCode, badRequestErrorName, message, 3)
}
func Unauthorized(message string) CodeError {
return NewWithDepth(unauthorizedErrorCode, unauthorizedErrorName, message, 3)
}
func Forbidden(message string) CodeError {
return NewWithDepth(forbiddenErrorCode, forbiddenErrorName, message, 3)
}
func NotFound(message string) CodeError {
return NewWithDepth(notFoundErrorCode, notFoundErrorName, message, 3)
}
func NotAcceptable(message string) CodeError {
return NewWithDepth(notAcceptableErrorCode, notAcceptableErrorName, message, 3)
}
func Timeout(message string) CodeError {
return NewWithDepth(timeoutErrorCode, timeoutErrorName, message, 3)
}
func ServiceError(message string) CodeError {
return NewWithDepth(serviceErrorCode, serviceErrorName, message, 3)
}
func NilError() CodeError {
return NewWithDepth(notFoundErrorCode, notFoundErrorName, nilErrorMessage, 3)
}
func NotImplemented(message string) CodeError {
return NewWithDepth(serviceNotImplementedErrorCode, serviceNotImplementedErrorName, message, 3)
}
func Unavailable(message string) CodeError {
return NewWithDepth(unavailableErrorCode, unavailableErrorName, message, 3)
}
func TooMayRequest(message string) CodeError {
return NewWithDepth(tooManyRequestsCode, tooManyRequestsName, message, 3)
}
func TooEarly(message string) CodeError {
return NewWithDepth(tooEarlyCode, tooEarlyName, message, 3)
}
func Warning(message string) CodeError {
return NewWithDepth(warnErrorCode, warnErrorName, message, 3)
}
func New(code int, name string, message string) CodeError {
return NewWithDepth(code, name, message, 3)
}
func NewWithDepth(code int, name string, message string, skip int) CodeError {
return CodeErrorImpl{
Id_: xid.New().String(),
Code_: code,
Name_: name,
Message_: message,
Meta_: nil,
Stacktrace_: newStacktrace(skip),
Cause_: nil,
}
}
func Wrap(err error) (codeErr CodeError) {
if err == nil {
codeErr = NewWithDepth(serviceErrorCode, serviceErrorName, "can not map nil to CodeError", 3)
return
}
e, ok := err.(CodeError)
if ok {
codeErr = e
return
}
codeErr = NewWithDepth(serviceErrorCode, serviceErrorName, err.Error(), 3)
return
}
func Decode(p []byte) (err CodeError) {
v := CodeErrorImpl{}
decodeErr := json.Unmarshal(p, &v)
if decodeErr != nil {
err = Warning("decode code error failed").WithCause(decodeErr)
return
}
err = v
return
}
type CodeErrorImpl struct {
Id_ string `json:"id,omitempty" avro:"id"`
Code_ int `json:"code,omitempty" avro:"code"`
Name_ string `json:"name,omitempty" avro:"name"`
Message_ string `json:"message,omitempty" avro:"message"`
Meta_ Meta `json:"meta,omitempty" avro:"meta"`
Stacktrace_ Stacktrace `json:"stacktrace,omitempty" avro:"stacktrace"`
Cause_ *CodeErrorImpl `json:"cause,omitempty" avro:"cause"`
}
func (e CodeErrorImpl) Id() string {
return e.Id_
}
func (e CodeErrorImpl) Code() int {
return e.Code_
}
func (e CodeErrorImpl) Name() string {
return e.Name_
}
func (e CodeErrorImpl) Message() string {
return e.Message_
}
func (e CodeErrorImpl) Stacktrace() (fn string, file string, line int) {
fn = e.Stacktrace_.Fn
file = e.Stacktrace_.File
line = e.Stacktrace_.Line
return
}
func (e CodeErrorImpl) WithMeta(key string, value string) (err CodeError) {
e.Meta_ = e.Meta_.Add(key, value)
err = e
return
}
func (e CodeErrorImpl) WithCause(cause error) (err CodeError) {
if cause == nil {
err = e
return
}
ce, ok := cause.(CodeError)
if !ok {
joined, isJoined := cause.(JoinedErrors)
if isJoined {
errs := joined.Unwrap()
if len(errs) > 0 {
ce = NewWithDepth(serviceErrorCode, serviceErrorName, errs[0].Error(), 4)
for _, sub := range errs[1:] {
ce = ce.WithCause(sub)
}
}
} else {
ce = NewWithDepth(serviceErrorCode, serviceErrorName, cause.Error(), 4)
}
}
if e.Cause_ == nil {
ca := ce.(CodeErrorImpl)
e.Cause_ = &ca
} else {
ce = e.Cause_.WithCause(ce)
ca := ce.(CodeErrorImpl)
e.Cause_ = &ca
}
err = e
return
}
func (e CodeErrorImpl) Contains(err error) (has bool) {
if err == nil {
return
}
codeErr, ok := err.(CodeError)
if ok {
if e.Message() == codeErr.Message() {
has = true
}
} else {
if e.Message() == err.Error() {
has = true
} else {
has = errors.Is(e, err)
}
}
if !has && e.Cause_ != nil {
has = e.Cause_.Contains(err)
}
return
}
func (e CodeErrorImpl) Error() string {
return e.String()
}
func (e CodeErrorImpl) String() string {
return fmt.Sprintf("%+v", e)
}
func (e CodeErrorImpl) Format(state fmt.State, verb rune) {
switch verb {
case 'v':
switch {
case state.Flag('+'):
buf := bytebufferpool.Get()
_, _ = buf.WriteString("\n>>>>>>>>>>>>>\n")
format(buf, e)
_, _ = buf.WriteString("<<<<<<<<<<<<<\n")
content := buf.Bytes()[:buf.Len()-1]
bytebufferpool.Put(buf)
_, _ = fmt.Fprintf(state, "%s", content)
default:
_, _ = fmt.Fprintf(state, "%s", e.Message())
}
default:
_, _ = fmt.Fprintf(state, "%s", e.Message())
}
}
func MakeErrors() Errors {
return make([]CodeError, 0, 1)
}
type Errors []CodeError
func (e *Errors) Append(err error) {
*e = append(*e, Wrap(err))
}
func (e *Errors) Error() (err error) {
if len(*e) == 0 {
return
}
e0 := (*e)[0]
if len(*e) > 1 {
for i := 1; i < len(*e); i++ {
e0 = e0.WithCause((*e)[i])
}
}
err = e0
return
}
func Contains(a error, b error) (has bool) {
if a == nil {
return
}
if b == nil {
return
}
codeErrorA, aOk := a.(CodeError)
if aOk {
has = codeErrorA.Contains(b)
return
}
has = errors.Is(a, b)
return
}
func As(err error) (e CodeError, ok bool) {
if err == nil {
return
}
e, ok = err.(CodeError)
return
}
type JoinedErrors interface {
Unwrap() []error
}