-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
283 lines (229 loc) · 5.99 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
package errors
import (
"fmt"
)
type errorString struct {
message string
}
// Error implements the standard library error interface.
func (s *errorString) Error() string {
return s.message
}
// New returns an error with the supplied message without cause.
func New(message string) error {
return &errorString{
message: message,
}
}
// Newf returns an error without cause with the formats according to a format specifier.
func Newf(format string, args ...interface{}) error {
message := fmt.Sprintf(format, args...)
return &errorString{
message: message,
}
}
// Is implements future error.Is functionality.
// An Error is equivalent if err message identical.
func (s *errorString) Is(err error) bool {
return s.message == err.Error()
}
type withMessage struct {
message string
err error
}
// Error implements the standard library error interface.
func (wm *withMessage) Error() string {
return wm.message
}
// Unwrap implements errors.Unwrap for Error.
func (wm *withMessage) Unwrap() error {
return wm.err
}
// Wrap returns an error annotating
// at the point Wrap is called, and the supplied message.
// If err is nil, Wrap returns nil.
func Wrap(err error, message string) error {
if err == nil {
return nil
}
msg := fmt.Sprintf("%s: %s", message, err)
return &withMessage{
// message is the full concatenate error message (top to bottom)
message: msg,
// err is the original error
err: err,
}
}
// Wrapf returns an error annotating
// at the point Wrapf is called, and the supplied message.
// If err is nil, Wrapf returns nil.
func Wrapf(err error, format string, args ...any) error {
if err == nil {
return nil
}
message := fmt.Sprintf(format, args...)
return Wrap(err, message)
}
type withError struct {
// message is the full concatenate error message (top to bottom)
message string
// err is the supplied error most of the time the sentinel error.
err error
// cause is the original error.
cause error
}
// Error implements the standard library error interface.
func (we *withError) Error() string {
return we.message
}
// Unwrap implements errors.Unwrap for Error.
func (we *withError) Unwrap() error {
return we.err
}
// Cause returns the underlying cause of error.
func (we *withError) Cause() error {
return we.cause
}
// WrapError returns an error annotating err with cause
// at the point WrapWithError is called, and the supplied err.
//
// If err is nil, WrapError returns supplied err.
// If supplied err is nil, WrapWithError returns err.
func WrapError(err error, supplied error) error {
if err == nil {
return supplied
}
if supplied == nil {
return err
}
msg := fmt.Sprintf("%s: %s", supplied, err)
return &withError{
message: msg,
err: supplied,
cause: err,
}
}
// Is implements future error.Is functionality.
// An Error is equivalent if err message or any of the underlying cause message are identical.
func (we *withError) Is(target error) bool {
if Is(we.err, target) {
return true
}
cause := Cause(we)
if cause == nil {
return false
}
return Is(cause, target)
}
// Cause returns the underlying cause of the error, if possible.
// An error value has a cause if it implements the following
// interface:
//
// type causer interface {
// Cause() error
// }
//
// If the error does not implement Cause, the error nil will
// be returned. If the error is nil, nil will be returned without further
// investigation.
func Cause(err error) error {
type causer interface {
Cause() error
}
cause, ok := err.(causer)
if !ok {
return nil
}
return cause.Cause()
}
// tuples is a slice of keys and values, e.g. {"key1", 1, "key2", "val2"}.
type tuples []interface{}
// 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
}
type enrichedError struct {
err error
keysAndValues tuples
}
// Error implements the standard library error interface.
func (ee *enrichedError) Error() string {
return ee.err.Error()
}
// Unwrap implements errors.Unwrap for Error.
func (ee *enrichedError) Unwrap() error {
return ee.err
}
// Tuples returns structured data of error in form of loosely-typed key-value pairs.
func (ee *enrichedError) Tuples() []interface{} {
return keysAndValues(ee)
}
func keysAndValues(err error) []interface{} {
var kv []interface{}
//nolint:errorlint
if ee, ok := err.(*enrichedError); ok {
kv = append(kv, ee.keysAndValues...)
}
uErr := Unwrap(err)
if uErr == nil {
return kv
}
kv = append(kv, keysAndValues(uErr)...)
cause := Cause(err)
if cause == nil {
return kv
}
kv = append(kv, keysAndValues(cause)...)
return kv
}
// Fields returns structured data of error as a map.
func (ee *enrichedError) Fields() map[string]interface{} {
return ee.keysAndValues.fields()
}
// Enrich takes in a basic error object and appends additional relevant fields, enriching the error message to help
// diagnose and resolve the error more effectively.
//
// If err is nil, Enrich returns nil.
// If keysAndValues is nil, Enrich returns err.
// If err is enrichedError, the keysAndValues will be appended to the existing keysAndValues.
func Enrich(err error, keysAndValues ...interface{}) error {
if err == nil {
return nil
}
// keysAndValues must be a list of key-value pairs.
if len(keysAndValues)%2 != 0 {
return err
}
return &enrichedError{
err: err,
keysAndValues: keysAndValues,
}
}
// EnrichWrapError returns an enrichedError error annotating err with cause.
// @see WrapWithError and Enrich.
func EnrichWrapError(err error, supplied error, keysAndValues ...interface{}) error {
return Enrich(WrapError(err, supplied), keysAndValues...)
}