-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_error.go
More file actions
203 lines (179 loc) · 5.13 KB
/
stack_error.go
File metadata and controls
203 lines (179 loc) · 5.13 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
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xerr/blob/main/LICENSE.
package xerr
import (
"fmt"
"io"
"runtime"
"strconv"
)
// maxStackFrames is the maximum depth of callstack.
const maxStackFrames = 32
// stackError is an error enriched with callstack.
type stackError struct {
// origErr is the original error, if this error wraps another one.
origErr error
// stackPCs holds the callstack program counters.
stackPCs []uintptr
// msg is this error's message.
msg string
}
// Error returns the error's message.
// Implements std error interface.
//
// The returned value has the form <stackError.msg>: <stackError.origErr.Error()>,
// any of the 2 parts may be missing.
func (err stackError) Error() string {
message := err.msg
if err.origErr != nil {
if message == "" {
message = err.origErr.Error()
} else {
message += ": " + err.origErr.Error()
}
}
return message
}
// Format implements [fmt.Formatter].
// The following verbs are supported:
//
// %s print the error. If the error has an original error, it will be
// printed recursively.
// %v same behaviour as %s.
// %+v extended format. Each frame of the error's call stack will
// be printed in detail.
func (err stackError) Format(f fmt.State, verb rune) {
switch verb {
case 'v':
if f.Flag('+') {
err.writeMsg(f)
for _, pc := range err.stackPCs {
fnName, file, line := getFrame(pc - 1)
if !skipFrame(fnName, file) {
writeFrame(f, fnName, file, line)
}
}
return
}
fallthrough
case 's':
err.writeMsg(f)
}
}
// Unwrap returns original error (can be nil).
// It implements [errors.Is] / [errors.As] APIs.
func (err stackError) Unwrap() error {
return err.origErr
}
// writeMsg writes the error message.
// Used this instead of directly io.WriteString(w, err.Error()) to save some extra memory allocation.
func (err stackError) writeMsg(w io.Writer) {
_, _ = io.WriteString(w, err.msg)
if err.origErr != nil {
if err.msg != "" {
_, _ = io.WriteString(w, ": ")
}
_, _ = io.WriteString(w, err.origErr.Error())
}
}
// New returns an error with the supplied message.
// New also records the stack trace at the point it was called.
func New(msg string) error {
return &stackError{
msg: msg,
stackPCs: getCallStack(maxStackFrames),
}
}
// Errorf formats according to a format specifier and returns the string
// as a value that satisfies error.
// Errorf also records the stack trace at the point it was called.
func Errorf(format string, args ...any) error {
return &stackError{
msg: fmt.Sprintf(format, args...),
stackPCs: getCallStack(maxStackFrames),
}
}
// Wrap returns an error annotating err with a stack trace
// at the point Wrap is called, and the supplied message.
// If err is nil, Wrap returns nil.
// If err is another stack trace aware error, the final stack trace will
// consists of original error's stack trace + 1 trace of current Wrap call.
func Wrap(err error, msg string) error {
if err == nil {
return nil
}
var stackPCs []uintptr
if sErr, ok := err.(*stackError); ok {
stackPCs = append(getCallStack(1), sErr.stackPCs...)
} else {
stackPCs = getCallStack(maxStackFrames)
}
return &stackError{
origErr: err,
msg: msg,
stackPCs: stackPCs,
}
}
// Wrapf returns an error annotating err with a stack trace
// at the point Wrapf is called, and the message formatted according to a
// format specifier.
// If err is nil, Wrapf returns nil.
// If err is another stack trace aware error, the final stack trace will
// consists of original error's stack trace + 1 trace of current Wrapf call.
func Wrapf(err error, format string, args ...any) error {
if err == nil {
return nil
}
var stackPCs []uintptr
if sErr, ok := err.(*stackError); ok {
stackPCs = append(getCallStack(1), sErr.stackPCs...)
} else {
stackPCs = getCallStack(maxStackFrames)
}
return &stackError{
origErr: err,
msg: fmt.Sprintf(format, args...),
stackPCs: stackPCs,
}
}
// getCallStack return a slice of program counters of function invocations
// on the calling goroutine's stack.
func getCallStack(maxDepth int) []uintptr {
pcs := make([]uintptr, maxDepth)
n := runtime.Callers(3, pcs)
return pcs[:n]
}
// writeFrame writes the given frame to the specified writer.
//
// The format in which is written is:
//
// <functionName>
// <file>:<line>
//
// Example:
//
// github.com/actforgood/xerr_test.TestX
// /Users/bogdan/work/go/xerr/errors_test.go:68
func writeFrame(w io.Writer, fnName, file string, line int) {
_, _ = io.WriteString(w, "\n")
if frameFnNameProcessor != nil {
_, _ = io.WriteString(w, frameFnNameProcessor(fnName))
} else {
_, _ = io.WriteString(w, fnName)
}
_, _ = io.WriteString(w, "\n\t")
_, _ = io.WriteString(w, file)
_, _ = io.WriteString(w, ":")
_, _ = io.WriteString(w, strconv.FormatInt(int64(line), 10))
}
// getFrame returns function, file and line for a program counter.
func getFrame(pc uintptr) (fnName, file string, line int) {
fn := runtime.FuncForPC(pc)
if fn != nil {
fnName = fn.Name()
file, line = fn.FileLine(pc)
}
return
}