-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
373 lines (300 loc) · 7.53 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package ctxd
import (
"context"
"encoding"
"encoding/json"
"errors"
"fmt"
)
// LogFunc defines contextualized logger function.
type LogFunc func(ctx context.Context, msg string, keysAndValues ...interface{})
// LogError pushes error value to a contextualized logger method.
//
// If err is nil, LogError produces no operation.
// LogError function matches Logger methods, e.g. Error.
func LogError(ctx context.Context, err error, l LogFunc) {
if err == nil {
return
}
var se StructuredError
if errors.As(err, &se) {
// Discarding keys and values from context as error already has full set of fields prepared on invocation.
l(ClearFields(ctx), se.Error(), se.Tuples()...)
return
}
l(ctx, err.Error())
}
// StructuredError defines error with message and data.
type StructuredError interface {
// Error returns message of error.
Error() string
// Tuples returns structured data of error in form of loosely-typed key-value pairs.
Tuples() []interface{}
// Fields returns structured data of error as a map.
Fields() map[string]interface{}
}
type wrappedError struct {
message string
err error
}
func (we wrappedError) Unwrap() error {
return we.err
}
func (we wrappedError) Error() string {
return we.message + ": " + we.err.Error()
}
// WrapError returns an error annotated with message and structured data.
//
// If err is nil, WrapError returns nil.
// LogError fields from context are also added to error structured data.
func WrapError(ctx context.Context, err error, message string, keysAndValues ...interface{}) error {
if err == nil {
return nil
}
if message != "" {
err = wrappedError{
err: err,
message: message,
}
}
se, ok := newError(ctx, err, keysAndValues...)
if ok {
return wrappedStructuredError{
structuredError: se,
}
}
return err
}
// NewError creates error with optional structured data.
//
// LogError fields from context are also added to error structured data.
func NewError(ctx context.Context, message string, keysAndValues ...interface{}) error {
//nolint:goerr113 // Static errors can be used with WrapError.
err := errors.New(message)
se, ok := newError(ctx, err, keysAndValues...)
if ok {
return se
}
return err
}
// Tuples is a slice of keys and values, e.g. {"key1", 1, "key2", "val2"}.
type Tuples []interface{}
type structuredError struct {
err error
keysAndValues Tuples
}
type wrappedStructuredError struct {
structuredError
}
// Unwrap implements errors wrapper.
func (wse wrappedStructuredError) Unwrap() error {
return wse.err
}
// Fields creates a map from key-value pairs.
func (t Tuples) Fields() map[string]interface{} {
if len(t) == 0 {
return nil
}
result := make(map[string]interface{}, len(t))
var (
label string
ok bool
)
for i, l := range t {
if label == "" {
label, ok = l.(string)
if !ok || label == "" {
result["malformedFields"] = []interface{}(t[i:])
break
}
} else {
result[label] = l
label = ""
}
}
if label != "" {
result["malformedFields"] = []interface{}{label}
}
return result
}
// Fields returns structured data of error as a map.
func (se structuredError) Fields() map[string]interface{} {
return se.keysAndValues.Fields()
}
// Error returns message and data serialized to a string.
func (se structuredError) String() string {
err := se.err.Error()
var (
label string
ok bool
)
for i, l := range se.keysAndValues {
if label == "" {
label, ok = l.(string)
if !ok {
err += fmt.Sprintf(", malformed fields: %+v", se.keysAndValues[i:])
break
}
} else {
err += fmt.Sprintf(", %s: %+v", label, l)
label = ""
}
}
return err
}
// Error returns message of error.
func (se structuredError) Error() string {
return se.err.Error()
}
// KeysAndValues returns structured data of error in form of loosely-typed key-value pairs.
func (se structuredError) Tuples() []interface{} {
return se.keysAndValues[0:len(se.keysAndValues):len(se.keysAndValues)]
}
func newError(ctx context.Context, err error, keysAndValues ...interface{}) (structuredError, bool) {
var (
se StructuredError
kv = keysAndValues
tuples []interface{}
ctxFields []interface{}
)
if errors.As(err, &se) {
tuples = se.Tuples()
}
ctxFields = Fields(ctx)
if len(tuples)+len(ctxFields) > 0 {
kv = make([]interface{}, 0, len(kv)+len(tuples)+len(ctxFields))
kv = append(kv, tuples...)
kv = append(kv, keysAndValues...)
kv = append(kv, ctxFields...)
}
if len(kv) > 1 {
return structuredError{
err: err,
keysAndValues: kv,
}, true
}
return structuredError{}, false
}
var (
_ encoding.TextMarshaler = structuredError{}
_ json.Marshaler = structuredError{}
)
func (se structuredError) MarshalText() ([]byte, error) {
return []byte(se.err.Error()), nil
}
func (se structuredError) MarshalJSON() ([]byte, error) {
return json.Marshal(se.err.Error())
}
// SentinelError is a constant error.
//
// See https://dave.cheney.net/2016/04/07/constant-errors for more details.
type SentinelError string
// Error returns error message.
func (e SentinelError) Error() string {
return string(e)
}
// LabeledError adds indicative errors to an error wrap.
//
// Labels could be checked with errors.Is, errors.As.
// Error message remains the same with original error.
func LabeledError(err error, labels ...error) error {
return labeledError{
err: err,
labels: labels,
}
}
type labeledError struct {
err error
labels []error
}
// Error returns message.
func (le labeledError) Error() string {
return le.err.Error()
}
// Is returns true if err matches original error or any of labels.
func (le labeledError) Is(err error) bool {
if errors.Is(le.err, err) {
return true
}
for _, l := range le.labels {
if errors.Is(err, l) {
return true
}
}
return false
}
// As returns true if original error or any of labels can be assigned to v.
//
// If multiple assignations are possible, only first one is performed.
func (le labeledError) As(v interface{}) bool {
if errors.As(le.err, v) {
return true
}
for _, l := range le.labels {
if errors.As(l, v) {
return true
}
}
return false
}
// Unwrap returns original error.
func (le labeledError) Unwrap() error {
return le.err
}
// MultiError creates an error with multiple unwrappables.
//
// Secondary errors could be checked with errors.Is, errors.As.
// Error message remains the same with primary error.
//
// Multi errors can be used to augment error with multiple
// checkable perks, without a limitation of single wrapping inheritance.
func MultiError(primary error, secondary ...error) error {
return multi{
primary: primary,
secondary: secondary,
}
}
type multi struct {
primary error
secondary []error
}
// Error returns message.
func (le multi) Error() string {
if le.primary == nil {
if len(le.secondary) > 0 {
return le.secondary[0].Error()
}
return "empty multi error"
}
return le.primary.Error()
}
// Is returns true if err matches primary error or any of secondary.
func (le multi) Is(err error) bool {
if errors.Is(le.primary, err) {
return true
}
for _, l := range le.secondary {
if errors.Is(err, l) {
return true
}
}
return false
}
// As returns true if primary error or any of secondary can be assigned to v.
//
// If multiple assignations are possible, only first one is performed.
func (le multi) As(v interface{}) bool {
if errors.As(le.primary, v) {
return true
}
for _, l := range le.secondary {
if errors.As(l, v) {
return true
}
}
return false
}
// Unwrap returns primary error.
func (le multi) Unwrap() error {
return le.primary
}