-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
257 lines (215 loc) · 5.9 KB
/
Copy patherror.go
File metadata and controls
257 lines (215 loc) · 5.9 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
package errors
import (
"errors"
"fmt"
"io"
"iter"
"maps"
"reflect"
"runtime"
"slices"
"strings"
)
// Error is an error enriched with structured key-value fields, an optional
// cause chain, and a captured stack trace. All methods that add data return a
// new copy; the original is never mutated.
type Error struct {
err error
data map[string]any
cause error
stack []uintptr
}
// New creates an Error from the given text and captures the current stack trace.
func New(text string) *Error {
return &Error{
err: errors.New(text),
stack: callers(1),
}
}
// Newf creates an Error from a formatted string and captures the current stack trace.
func Newf(format string, v ...any) *Error {
return &Error{
err: fmt.Errorf(format, v...),
stack: callers(1),
}
}
// Wrap converts an error into an *Error and captures the current stack trace.
// If err is nil, Wrap returns nil. If err is already an *Error it is returned
// unchanged. Otherwise the error is wrapped directly.
//
// Warning: the returned *Error nil is a typed nil pointer. When assigned to
// or returned as an error interface it will not equal nil. Prefer checking the
// error before passing it to Wrap rather than checking the result afterwards.
func Wrap(err error) *Error {
if err == nil {
return nil
}
if dataErr, ok := err.(*Error); ok {
return dataErr
}
return &Error{
err: err,
stack: callers(1),
}
}
// Error returns the error message string. A nil *Error reports "<nil>".
func (e *Error) Error() string {
if e == nil || e.err == nil {
return "<nil>"
}
return e.err.Error()
}
// Fields returns a copy of the structured key-value data attached to this error.
// Mutating the result does not affect the error.
func (e *Error) Fields() map[string]any {
if e == nil {
return nil
}
return maps.Clone(e.data)
}
// WithField returns a copy of the error with the given key-value field added.
// The original error is not modified.
func (e *Error) WithField(key string, value any) *Error {
return e.WithFields(map[string]any{key: value})
}
// WithFields returns a copy of the error with the given fields merged in.
// The original error is not modified. A nil *Error returns nil, so chaining after Wrap(nil) does not panic.
func (e *Error) WithFields(values map[string]any) *Error {
if e == nil {
return nil
}
data := make(map[string]any, len(e.data)+len(values))
maps.Copy(data, e.data)
maps.Copy(data, values)
return &Error{
err: e.err,
data: data,
cause: e.cause,
stack: e.stack,
}
}
// WithCause returns a copy of the error with the given cause attached.
// The cause is returned by Unwrap, making it visible to errors.Is and errors.As. A nil *Error returns nil.
func (e *Error) WithCause(err error) *Error {
if e == nil {
return nil
}
return &Error{
err: e.err,
data: e.data,
cause: err,
stack: e.stack,
}
}
// Unwrap returns the cause if one was set via WithCause; otherwise it returns
// the underlying error created by New, Newf, or Wrap.
func (e *Error) Unwrap() error {
if e == nil {
return nil
}
if e.cause != nil {
return e.cause
}
return e.err
}
// Is reports whether e matches target. Two *Error values are considered equal
// when their messages match and every field present in target also appears in
// e with the same value. This allows errors.Is to find a sentinel Error
// anywhere in a chain, optionally scoped by fields.
//
// Field values of different types never match. Uncomparable values are
// compared with reflect.DeepEqual, so function fields only match when both are nil.
func (e *Error) Is(target error) bool {
var err *Error
if e == nil || !errors.As(target, &err) || err == nil {
return false
}
if e.Error() != err.Error() {
return false
}
for k, v := range err.data {
if !equal(e.data[k], v) {
return false
}
}
return true
}
// Format implements fmt.Formatter. The %s, %q and %v verbs print the message
// alone; %+v additionally prints the fields, the cause chain and the stack
// trace.
func (e *Error) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
io.WriteString(s, e.details())
return
}
io.WriteString(s, e.Error())
case 's':
io.WriteString(s, e.Error())
case 'q':
fmt.Fprintf(s, "%q", e.Error())
default:
fmt.Fprintf(s, "%%!%c(*errors.Error=%s)", verb, e.Error())
}
}
// StackTrace returns the program counters captured when the error was created.
// See [Error.Frames] for the resolved call frames.
func (e *Error) StackTrace() []uintptr {
if e == nil {
return nil
}
return e.stack
}
// Frames resolves the captured stack trace into call frames, outermost call first.
// The sequence is empty when no stack was captured.
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
}
}
}
}
// details renders the message together with the fields, the cause and the
// stack trace, as printed by the %+v verb.
func (e *Error) details() string {
b := &strings.Builder{}
b.WriteString(e.Error())
if e == nil {
return b.String()
}
for _, k := range slices.Sorted(maps.Keys(e.data)) {
fmt.Fprintf(b, "\n\t%s=%v", k, e.data[k])
}
if e.cause != nil {
fmt.Fprintf(b, "\ncaused by: %v", e.cause)
}
for frame := range e.Frames() {
fmt.Fprintf(b, "\n\t%s\n\t\t%s:%d", frame.Function, frame.File, frame.Line)
}
return b.String()
}
// callers returns up to 32 program counters starting skip frames above the caller.
func callers(skip int) []uintptr {
stack := make([]uintptr, 32)
n := runtime.Callers(skip+2, stack)
return stack[:n]
}
// equal compares two field values without panicking on uncomparable types.
func equal(a, b any) bool {
t := reflect.TypeOf(a)
if t != reflect.TypeOf(b) {
return false
}
if t == nil || t.Comparable() {
return a == b
}
return reflect.DeepEqual(a, b)
}