-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwraperr.go
251 lines (234 loc) · 6.94 KB
/
wraperr.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
package wrapify
import (
"fmt"
"io"
)
// WithError returns an error with the supplied message and records the stack trace
// at the point it was called. The error contains the message and the stack trace
// which can be used for debugging or logging the error along with the call stack.
//
// Usage example:
//
// err := WithError("Something went wrong")
// fmt.Println(err) // "Something went wrong" along with stack trace
func WithError(message string) error {
return &underlying{
msg: message,
stack: Callers(),
}
}
// WithErrorf formats the given arguments according to the format specifier and
// returns the formatted string as an error. It also records the stack trace at
// the point it was called.
//
// Usage example:
//
// err := WithErrorf("Failed to load file %s", filename)
// fmt.Println(err) // "Failed to load file <filename>" along with stack trace
func WithErrorf(format string, args ...interface{}) error {
return &underlying{
msg: fmt.Sprintf(format, args...),
stack: Callers(),
}
}
// WithErrStack annotates an existing error with a stack trace at the point
// WithErrStack was called. If the provided error is nil, it simply returns nil.
//
// Usage example:
//
// err := errors.New("original error")
// errWithStack := WithErrStack(err)
// fmt.Println(errWithStack) // original error with stack trace
func WithErrStack(err error) error {
if err == nil {
return nil
}
return &underlyingStack{
err,
Callers(),
}
}
// WithErrWrap returns an error that annotates the provided error with a new message
// and a stack trace at the point WithErrWrap was called. If the provided error is nil,
// WithErrWrap returns nil.
//
// Usage example:
//
// err := errors.New("file not found")
// wrappedErr := WithErrWrap(err, "Failed to read the file")
// fmt.Println(wrappedErr) // "Failed to read the file: file not found" with stack trace
func WithErrWrap(err error, message string) error {
if err == nil {
return nil
}
err = &underlyingMessage{
cause: err,
msg: message,
}
return &underlyingStack{
err,
Callers(),
}
}
// WithErrWrapf returns an error that annotates the provided error with a formatted message
// and a stack trace at the point WithErrWrapf was called. If the provided error is nil,
// WithErrWrapf returns nil.
//
// Usage example:
//
// err := errors.New("file not found")
// wrappedErr := WithErrWrapf(err, "Failed to load file %s", filename)
// fmt.Println(wrappedErr) // "Failed to load file <filename>: file not found" with stack trace
func WithErrWrapf(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
err = &underlyingMessage{
cause: err,
msg: fmt.Sprintf(format, args...),
}
return &underlyingStack{
err,
Callers(),
}
}
// WithMessage annotates an existing error with a new message. If the error is nil,
// it returns nil.
//
// Usage example:
//
// err := errors.New("original error")
// errWithMessage := WithMessage(err, "Additional context")
// fmt.Println(errWithMessage) // "Additional context: original error"
func WithMessage(err error, message string) error {
if err == nil {
return nil
}
return &underlyingMessage{
cause: err,
msg: message,
}
}
// WithMessagef annotates an existing error with a formatted message. If the error
// is nil, it returns nil.
//
// Usage example:
//
// err := errors.New("original error")
// errWithMessage := WithMessagef(err, "Context: %s", "something went wrong")
// fmt.Println(errWithMessage) // "Context: something went wrong: original error"
func WithMessagef(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
return &underlyingMessage{
cause: err,
msg: fmt.Sprintf(format, args...),
}
}
// Cause traverses the error chain and returns the underlying cause of the error
// if it implements the `Cause()` method. If the error doesn't implement `Cause()`,
// it simply returns the original error. If the error is nil, nil is returned.
//
// Usage example:
//
// err := Wrap(errors.New("file not found"), "Failed to open file")
// causeErr := Cause(err)
// fmt.Println(causeErr) // "file not found"
//
// 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 original error 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
}
for err != nil {
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
}
return err
}
// Error implements the error interface for the `underlying` type, returning the
// message stored in the error object. It is used to retrieve the error message.
//
// Usage example:
//
// err := WithError("Something went wrong")
// fmt.Println(err.Error()) // "Something went wrong"
func (u *underlying) Error() string {
return u.msg
}
// Format formats the error according to the specified format verb. If the `+`
// flag is provided, it will format both the message and the stack trace. Otherwise,
// it will format just the message.
//
// Usage example:
//
// err := WithError("Something went wrong")
// fmt.Printf("%+v\n", err) // "Something went wrong" with stack trace
func (u *underlying) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
io.WriteString(s, u.msg)
u.stack.Format(s, verb)
return
}
fallthrough
case 's':
io.WriteString(s, u.msg)
case 'q':
fmt.Fprintf(s, "%q", u.msg)
}
}
// Cause returns the underlying error cause for the `underlyingStack` type.
func (u *underlyingStack) Cause() error { return u.error }
// Unwrap returns the underlying error for the `underlyingStack` type.
func (u *underlyingStack) Unwrap() error { return u.error }
// Format formats the error with the stack trace and the error message. If the
// `+` flag is set, it will include the stack trace as well.
//
// Usage example:
//
// err := Wrap(errors.New("file not found"), "Failed to open file")
// fmt.Printf("%+v\n", err) // "Failed to open file: file not found" with stack trace
func (u *underlyingStack) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v", u.Cause())
u.stack.Format(s, verb)
return
}
fallthrough
case 's':
io.WriteString(s, u.Error())
case 'q':
fmt.Fprintf(s, "%q", u.Error())
}
}
// Cause returns the underlying cause of the error for the `underlyingMessage` type.
func (u *underlyingMessage) Cause() error { return u.cause }
// Unwrap returns the underlying cause of the error for the `underlyingMessage` type.
func (u *underlyingMessage) Unwrap() error { return u.cause }
// Error returns the error message concatenated with the underlying error message
// for the `underlyingMessage` type.
//
// Usage example:
//
// err := WithMessage(errors.New("file not found"), "Failed to open file")
// fmt.Println(err) // "Failed to open file: file not found"
func (u *underlyingMessage) Error() string {
return u.msg + ": " + u.cause.Error()
}