-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
192 lines (166 loc) · 3.47 KB
/
error.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
package xerrors
import (
"errors"
"fmt"
"io"
"runtime"
"strings"
"go.uber.org/zap/zapcore"
)
type Error struct {
err error
msg string
stackTrace Frames
}
func (e *Error) Error() string {
b := new(strings.Builder)
b.WriteString(e.msg)
if e.err != nil {
if b.Len() != 0 {
b.WriteString(": ")
}
fmt.Fprint(b, e.err)
}
return b.String()
}
func (e *Error) Unwrap() error {
return e.err
}
func (e *Error) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if e.msg != "" {
io.WriteString(s, e.msg)
}
if e.err != nil {
if e.msg != "" {
io.WriteString(s, ": ")
}
fmt.Fprint(s, e.err)
}
if s.Flag('+') {
io.WriteString(s, "\n")
io.WriteString(s, e.StackTrace().String())
}
}
}
// StackTrace returns Frames that is most deeply frames in the error chain.
func (e *Error) StackTrace() Frames {
return StackTrace(e)
}
// New returns an error.
func New(msg string) error {
return &Error{msg: msg}
}
// Newf returns an error with formatted text.
func Newf(format string, a ...any) error {
return &Error{msg: fmt.Sprintf(format, a...)}
}
// NewWithStack returns an error with a stack trace.
func NewWithStack(msg string) error {
return &Error{msg: msg, stackTrace: caller()}
}
// NewfWithStack returns an error with formatted text and a stack trace.
func NewfWithStack(format string, a ...any) error {
return &Error{msg: fmt.Sprintf(format, a...), stackTrace: caller()}
}
// WithStack annotates err with a stack trace.
// If err is nil, WithStack returns nil.
func WithStack(err error) error {
if err == nil {
return nil
}
if StackTrace(err) == nil {
return &Error{err: err, stackTrace: caller()}
}
return err
}
func WithMessage(err error, msg string) error {
var st []uintptr
if StackTrace(err) == nil {
st = caller()
}
return &Error{msg: msg, err: err, stackTrace: st}
}
func WithMessagef(err error, format string, a ...any) error {
var st []uintptr
if StackTrace(err) == nil {
st = caller()
}
return &Error{msg: fmt.Sprintf(format, a...), err: err, stackTrace: st}
}
type Frames []uintptr
var _ zapcore.ArrayMarshaler = Frames{}
func StackTrace(err error) Frames {
var sErr *Error
if !errors.As(err, &sErr) {
return nil
}
err = sErr
var frames Frames
for {
v, ok := err.(*Error)
if ok {
if len(frames) < len(v.stackTrace) {
frames = v.stackTrace
}
}
if err = errors.Unwrap(err); err == nil {
break
}
}
return frames
}
func (f Frames) String() string {
s := &strings.Builder{}
frames := runtime.CallersFrames(f)
for {
frame, more := frames.Next()
if frame.Function != "" {
fmt.Fprintf(s, "%s\n", frame.Function)
}
if frame.File != "" {
fmt.Fprintf(s, " %s:%d\n", frame.File, frame.Line)
}
if !more {
break
}
}
return s.String()
}
func (f Frames) Frame(i int) *Frame {
return newFrame(f[i])
}
func (f Frames) MarshalLogArray(e zapcore.ArrayEncoder) error {
frames := runtime.CallersFrames(f)
for {
frame, more := frames.Next()
e.AppendString(fmt.Sprintf("%s:%s:%d", frame.Function, frame.File, frame.Line))
if !more {
break
}
}
return nil
}
type Frame struct {
Name string
File string
Line int
}
func newFrame(f uintptr) *Frame {
fn := runtime.FuncForPC(f)
file, line := fn.FileLine(f)
return &Frame{
Name: fn.Name(),
File: file,
Line: line,
}
}
func (f *Frame) String() string {
return fmt.Sprintf("%s %s:%d", f.Name, f.File, f.Line)
}
func caller() []uintptr {
pcs := make([]uintptr, 32)
n := runtime.Callers(3, pcs)
return pcs[:n]
}