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
|
package errors
import "fmt"
// ErrorContext defines a wrappable error with the ability to hold extra contextual information
type ErrorContext interface {
// implement base error interface
error
// Is identifies whether the receiver contains / is the target
Is(error) bool
// Unwrap reveals the underlying wrapped error (if any!)
Unwrap() error
// Value attempts to fetch contextual data for given key from this ErrorContext
Value(string) (interface{}, bool)
// Append allows adding contextual data to this ErrorContext
Append(...KV) ErrorContext
// Data returns the contextual data structure associated with this ErrorContext
Data() ErrorData
}
// New returns a new ErrorContext created from string
func New(msg string) ErrorContext {
return stringError(msg)
}
// Newf returns a new ErrorContext created from format string
func Newf(s string, a ...interface{}) ErrorContext {
return stringError(fmt.Sprintf(s, a...))
}
// Wrap ensures supplied error is an ErrorContext, wrapping if necessary
func Wrap(err error) ErrorContext {
// Nil error, do nothing
if err == nil {
return nil
}
// Check if this is already wrapped somewhere in stack
if xerr, ok := err.(*errorContext); ok {
return xerr
} else if As(err, &xerr) {
// This is really not an ideal situation,
// but we try to make do by salvaging the
// contextual error data from earlier in
// stack, setting current error to the top
// and setting the unwrapped error to inner
return &errorContext{
data: xerr.data,
innr: Unwrap(err),
err: err,
}
}
// Return new Error type
return &errorContext{
data: NewData(),
innr: nil,
err: err,
}
}
// WrapMsg wraps supplied error as inner, returning an ErrorContext
// with a new outer error made from the supplied message string
func WrapMsg(err error, msg string) ErrorContext {
// Nil error, do nothing
if err == nil {
return nil
}
// Check if this is already wrapped
var xerr *errorContext
if As(err, &xerr) {
return &errorContext{
data: xerr.data,
innr: err,
err: New(msg),
}
}
// Return new wrapped error
return &errorContext{
data: NewData(),
innr: err,
err: stringError(msg),
}
}
// WrapMsgf wraps supplied error as inner, returning an ErrorContext with
// a new outer error made from the supplied message format string
func WrapMsgf(err error, msg string, a ...interface{}) ErrorContext {
return WrapMsg(err, fmt.Sprintf(msg, a...))
}
// ErrorData attempts fetch ErrorData from supplied error, returns nil otherwise
func Data(err error) ErrorData {
x, ok := err.(ErrorContext)
if ok {
return x.Data()
}
return nil
}
// stringError is the simplest ErrorContext implementation
type stringError string
func (e stringError) Error() string {
return string(e)
}
func (e stringError) Is(err error) bool {
se, ok := err.(stringError)
return ok && e == se
}
func (e stringError) Unwrap() error {
return nil
}
func (e stringError) Value(key string) (interface{}, bool) {
return nil, false
}
func (e stringError) Append(kvs ...KV) ErrorContext {
data := NewData()
data.Append(kvs...)
return &errorContext{
data: data,
innr: nil,
err: e,
}
}
func (e stringError) Data() ErrorData {
return nil
}
// errorContext is the *actual* ErrorContext implementation
type errorContext struct {
// data contains any appended context data, there will only ever be one
// instance of data within an ErrorContext stack
data ErrorData
// innr is the inner wrapped error in this structure, it is only accessible
// via .Unwrap() or via .Is()
innr error
// err is the top-level error in this wrapping structure, we identify
// as this error type via .Is() and return its error message
err error
}
func (e *errorContext) Error() string {
return e.err.Error()
}
func (e *errorContext) Is(err error) bool {
return Is(e.err, err) || Is(e.innr, err)
}
func (e *errorContext) Unwrap() error {
return e.innr
}
func (e *errorContext) Value(key string) (interface{}, bool) {
return e.data.Value(key)
}
func (e *errorContext) Append(kvs ...KV) ErrorContext {
e.data.Append(kvs...)
return e
}
func (e *errorContext) Data() ErrorData {
return e.data
}
|