Repository navigation
Expand file tree
/
Copy patherror.go
More file actions
300 lines (242 loc) · 6.64 KB
/
Copy patherror.go
File metadata and controls
300 lines (242 loc) · 6.64 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
package errors
import (
"errors"
"fmt"
"io"
"iter"
"log/slog"
"maps"
"runtime"
"slices"
"strings"
)
const maxFrames = 32
// Error is an error with key-value fields, an optional cause and a stack trace. The zero value
// has no underlying error, cause or stack trace, and its message is <nil>.
type Error struct {
err error
cause error
fields map[string]any
stack []uintptr
}
// New returns an Error with the given text and the current stack trace.
func New(text string) *Error {
return wrap(errors.New(text))
}
// Sentinel returns a plain error without a stack trace, for package-level variables. Wrap
// derives an Error from it.
func Sentinel(text string) error {
return errors.New(text)
}
// Newf returns Wrap(fmt.Errorf(format, args...)).
func Newf(format string, args ...any) *Error {
return wrap(fmt.Errorf(format, args...))
}
// Wrap returns err itself if it is an Error, else a new Error around it with the stack trace of
// the first Error in its Unwrap() error chain, or the current one if there is none. A nil err
// returns nil.
func Wrap(err error) *Error {
if !exists(err) {
return nil
}
if e, ok := err.(*Error); ok {
return e
}
return wrap(err)
}
func wrap(err error) *Error {
return &Error{
err: err,
stack: stackOf(err),
}
}
// Error returns the message of the underlying error.
func (e *Error) Error() string {
if e == nil || e.err == nil {
return "<nil>"
}
return e.err.Error()
}
// Unwrap returns the underlying error, then the cause if there is one.
func (e *Error) Unwrap() []error {
if e == nil || e.err == nil {
return nil
}
if e.cause == nil {
return []error{e.err}
}
return []error{e.err, e.cause}
}
// Is reports whether target is an Error whose underlying error errors.Is finds in the error's
// underlying error or cause. A target without an underlying error matches only itself.
func (e *Error) Is(target error) bool {
other, ok := target.(*Error)
if !ok || other == nil || other.err == nil {
return false
}
return errors.Is(e, other.err)
}
// Fields returns the error's own fields, not those of its underlying error or cause.
func (e *Error) Fields() map[string]any {
if e == nil {
return nil
}
return maps.Clone(e.fields)
}
// WithField returns the error with the field added, replacing one with the same key. It panics
// for a nil receiver.
func (e *Error) WithField(key string, value any) *Error {
return e.WithFields(map[string]any{key: value})
}
// WithFields returns the error with the fields added, replacing those with the same keys. It
// panics for a nil receiver.
func (e *Error) WithFields(fields map[string]any) *Error {
derived := *e
derived.fields = make(map[string]any, len(e.fields)+len(fields))
maps.Copy(derived.fields, e.fields)
maps.Copy(derived.fields, fields)
return &derived
}
// WithCause returns the error with the cause attached. A second cause is joined with those
// attached into one flat MultiError; a nil cause or an empty collection attaches nothing. It
// panics for a nil receiver.
func (e *Error) WithCause(cause error) *Error {
causes := slices.Concat(members(e.cause), members(cause))
derived := *e
switch len(causes) {
case 0:
// no-op
case 1:
derived.cause = causes[0]
default:
derived.cause = Join(causes...)
}
return &derived
}
// StackTrace returns the program counters of the stack trace, innermost call first.
func (e *Error) StackTrace() []uintptr {
if e == nil {
return nil
}
return slices.Clone(e.stack)
}
// Frames returns the call frames of the stack trace, innermost call first.
func (e *Error) Frames() iter.Seq[runtime.Frame] {
return func(yield func(runtime.Frame) bool) {
if e == nil || len(e.stack) == 0 {
return
}
frames := runtime.CallersFrames(e.stack)
for {
frame, more := frames.Next()
if !yield(frame) || !more {
return
}
}
}
}
// Format prints the message for %s, %v and %q, the underlying error, the fields sorted by key,
// the stack trace and every cause for %+v, and Go syntax for %#v.
func (e *Error) Format(s fmt.State, verb rune) {
switch {
case verb == 'v' && s.Flag('#'):
_, _ = io.WriteString(s, e.GoString())
case verb == 'v' && s.Flag('+'):
_, _ = io.WriteString(s, e.details())
default:
_, _ = fmt.Fprintf(s, fmt.FormatString(s, verb), e.Error())
}
}
// GoString returns the error in Go syntax, without the stack trace.
func (e *Error) GoString() string {
if e == nil {
return "(*errors.Error)(nil)"
}
var items []string
if e.err != nil {
items = append(items, fmt.Sprintf("err:%#v", e.err))
}
if e.cause != nil {
items = append(items, fmt.Sprintf("cause:%#v", e.cause))
}
if len(e.fields) > 0 {
items = append(items, fmt.Sprintf("fields:%#v", e.fields))
}
return "&errors.Error{" + strings.Join(items, ", ") + "}"
}
// LogValue returns the error as a slog group of msg, a group of the fields sorted by key, and
// cause if there is one, without the stack trace.
func (e *Error) LogValue() slog.Value {
if e == nil {
return slog.AnyValue(nil)
}
fields := make([]slog.Attr, 0, len(e.fields))
for _, key := range slices.Sorted(maps.Keys(e.fields)) {
fields = append(fields, slog.Any(key, e.fields[key]))
}
attrs := []slog.Attr{
slog.String("msg", e.Error()),
slog.GroupAttrs("fields", fields...),
}
if e.cause != nil {
attrs = append(attrs, slog.Any("cause", e.cause))
}
return slog.GroupValue(attrs...)
}
func (e *Error) details() string {
if e == nil {
return "<nil>"
}
lines := []string{fmt.Sprintf("%+v", e.err)}
for _, key := range slices.Sorted(maps.Keys(e.fields)) {
lines = append(lines, indent(fmt.Sprintf("%s=%v", key, e.fields[key])))
}
for frame := range e.Frames() {
lines = append(lines, indent(fmt.Sprintf("%s\n\t%s:%d", frame.Function, frame.File, frame.Line)))
}
for _, cause := range members(e.cause) {
lines = append(lines, indent(fmt.Sprintf("caused by: %+v", cause)))
}
return strings.Join(lines, "\n")
}
func stackOf(err error) []uintptr {
for ; err != nil; err = errors.Unwrap(err) {
if inner, ok := err.(*Error); ok && inner != nil {
return inner.stack
}
}
return callers()
}
func members(err error) []error {
if !exists(err) {
return nil
}
errs, ok := err.(*MultiError)
if !ok {
return []error{err}
}
var flat []error
for _, member := range errs.Unwrap() {
flat = append(flat, members(member)...)
}
return flat
}
func exists(err error) bool {
switch err := err.(type) {
case nil:
return false
case *Error:
return err != nil
case *MultiError:
return err != nil
}
return true
}
func indent(text string) string {
return "\t" + strings.ReplaceAll(text, "\n", "\n\t")
}
func callers() []uintptr {
var stack [maxFrames]uintptr
n := runtime.Callers(5, stack[:])
return slices.Clone(stack[:n])
}