-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
165 lines (143 loc) · 3.22 KB
/
errors.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
package errors
import (
"encoding/json"
"errors"
"fmt"
"runtime"
"strings"
)
var (
_ error = (*errorWithStack)(nil)
_ json.Marshaler = (*errorWithStack)(nil)
_ fmt.Stringer = (stackTraces)(nil)
)
// MaxStackDepth is the maximum depth of the stack trace.
var MaxStackDepth = 50
// As is a wrapper for [errors.As].
//
// [errors.As]: https://pkg.go.dev/errors#As
func As(err error, target any) bool {
return errors.As(err, target)
}
// Is is a wrapper for [errors.Is].
//
// [errors.Is]: https://pkg.go.dev/errors#Is
func Is(err, target error) bool {
return errors.Is(err, target)
}
// Join is a wrapper for [errors.Join].
//
// [errors.Join]: https://pkg.go.dev/errors#Join
func Join(errs ...error) error {
return errors.Join(errs...)
}
// New is a wrapper for [errors.New].
//
// [errors.New]: https://pkg.go.dev/errors#New
func New(text string) error {
return errors.New(text)
}
// Unwrap is a wrapper for [errors.Unwrap].
//
// [errors.Unwrap]: https://pkg.go.dev/errors#Unwrap
func Unwrap(err error) error {
return errors.Unwrap(err)
}
// WithStack sets the stack trace for the given error.
func WithStack(err error) error {
if err == nil {
return nil
}
stack := make([]uintptr, MaxStackDepth)
length := runtime.Callers(2, stack[:])
var errws *errorWithStack
if errors.As(err, &errws) {
return err
} else {
errws = &errorWithStack{
Err: err,
stack: stack[:length],
}
}
return errws
}
// StackTraces returns the stack traces of the given error(s).
func StackTraces(err error) stackTraces {
je, ok := err.(joinError)
if ok {
// joined error
var traces stackTraces
errs := je.Unwrap()
for _, e := range errs {
traces = append(traces, StackTraces(e)...)
}
return traces
}
var errws *errorWithStack
if !errors.As(err, &errws) {
return stackTraces{}
}
errws.genFrames()
return stackTraces{errws}
}
type stackTraces []*errorWithStack
type errorWithStack struct {
Err error
Frames []Frame
stack []uintptr
}
type Frame struct {
Name string `json:"name"`
File string `json:"file"`
Line int `json:"line"`
}
func (traces stackTraces) String() string {
var sb strings.Builder
for i, errws := range traces {
if i > 0 {
sb.WriteString("\n")
}
sb.WriteString(errws.Error())
for _, frame := range errws.Frames {
sb.WriteString(fmt.Sprintf("\n%s\n\t%s:%d", frame.Name, frame.File, frame.Line))
}
}
return sb.String()
}
func (errws *errorWithStack) Error() string {
msg := errws.Err.Error()
return msg
}
func (errws *errorWithStack) Unwrap() error {
return errws.Err
}
func (errws *errorWithStack) MarshalJSON() ([]byte, error) {
s := struct {
Error string `json:"error"`
Frames []Frame `json:"frames"`
}{
Error: errws.Error(),
Frames: errws.Frames,
}
return json.Marshal(s)
}
type joinError interface {
Unwrap() []error
}
func (errws *errorWithStack) genFrames() {
if errws.Frames != nil {
return
}
errws.Frames = make([]Frame, len(errws.stack))
for i, pc := range errws.stack {
// ref: https://github.com/go-errors/errors/blob/83795c27c02f5cdeaf9a5c3c3fd2709376f20b79/Frame.go#L36-L37
fn := runtime.FuncForPC(pc - 1)
name := fn.Name()
file, line := fn.FileLine(pc - 1)
errws.Frames[i] = Frame{
Name: name,
File: file,
Line: line,
}
}
}