-
Notifications
You must be signed in to change notification settings - Fork 0
/
kerrors.go
220 lines (194 loc) · 4.41 KB
/
kerrors.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
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
package kerrors
import (
"errors"
"io"
"runtime"
"strconv"
"strings"
)
type (
// Error is an error with context
Error struct {
Message string
wrapped [2]error
skip int
}
// ErrorOpt is an error options function used by [New]
ErrorOpt = func(e *Error)
// ErrorWriter writes errors to an [io.Writer]
ErrorWriter interface {
WriteError(b io.Writer)
}
// ErrorMsger returns the error message
ErrorMsger interface {
ErrorMsg() string
}
)
// New creates a new [*Error]
func New(opts ...ErrorOpt) error {
e := &Error{
skip: 0,
}
for _, i := range opts {
i(e)
}
e.wrapped[1] = addStackTrace(e.Inner(), 1+e.skip)
return e
}
// WriteError implements [ErrorWriter]
func (e *Error) WriteError(b io.Writer) {
io.WriteString(b, e.Message)
if kind := e.Kind(); kind != nil {
io.WriteString(b, "\n[[\n")
if k, ok := kind.(ErrorWriter); ok {
k.WriteError(b)
} else {
io.WriteString(b, kind.Error())
}
io.WriteString(b, "\n]]")
}
if inner := e.Inner(); inner != nil {
io.WriteString(b, "\n--\n")
if k, ok := inner.(ErrorWriter); ok {
k.WriteError(b)
} else {
io.WriteString(b, inner.Error())
}
}
}
// Error implements error and recursively prints wrapped errors
func (e *Error) Error() string {
var b strings.Builder
e.WriteError(&b)
return b.String()
}
// ErrorMsg returns the error message
func (e *Error) ErrorMsg() string {
return e.Message
}
// Unwrap implements [errors.Unwrap]
func (e *Error) Unwrap() []error {
start := 0
end := 2
if e.wrapped[0] == nil {
start = 1
}
if e.wrapped[1] == nil {
end = 1
}
return e.wrapped[start:end]
}
// Kind returns the error kind
func (e *Error) Kind() error {
return e.wrapped[0]
}
// Inner returns the inner wrapped error
func (e *Error) Inner() error {
return e.wrapped[1]
}
// OptMsg returns an [ErrorOpt] that sets [Error] Message
func OptMsg(msg string) ErrorOpt {
return func(e *Error) {
e.Message = msg
}
}
// OptKind returns an [ErrorOpt] that sets [Error] Kind
func OptKind(kind error) ErrorOpt {
return func(e *Error) {
e.wrapped[0] = kind
}
}
// OptInner returns an [ErrorOpt] that sets [Error] Inner
func OptInner(inner error) ErrorOpt {
return func(e *Error) {
e.wrapped[1] = inner
}
}
// OptSkip returns an [ErrorOpt] that increments [Error] skip by a number of
// frames for stack trace
func OptSkip(skip int) ErrorOpt {
return func(e *Error) {
e.skip += skip
}
}
type (
// StackStringer returns a stacktrace string
StackStringer interface {
StackString() string
}
// StackTrace is an error stack trace
StackTrace struct {
n int
pc [128]uintptr
}
)
// NewStackTrace creates a new [*StackTrace]
func NewStackTrace(skip int) *StackTrace {
e := &StackTrace{}
e.n = runtime.Callers(2+skip, e.pc[:])
return e
}
// WriteError implements [ErrorWriter] and writes the stack trace
func (e *StackTrace) WriteError(b io.Writer) {
if e.n <= 0 {
return
}
frameIter := runtime.CallersFrames(e.pc[:1])
f, _ := frameIter.Next()
io.WriteString(b, f.Function)
io.WriteString(b, " ")
io.WriteString(b, f.File)
io.WriteString(b, ":")
io.WriteString(b, strconv.Itoa(f.Line))
}
// Error implements error and prints the stack trace
func (e *StackTrace) Error() string {
var b strings.Builder
e.WriteError(&b)
return b.String()
}
func (e *StackTrace) PC() []uintptr {
return e.pc[:e.n]
}
// StackString implements [StackStringer] and formats each frame of the stack
// trace with the default format
func (e *StackTrace) StackString() string {
if e.n <= 0 {
return ""
}
var b strings.Builder
frameIter := runtime.CallersFrames(e.PC())
for {
f, more := frameIter.Next()
b.WriteString(f.Function)
b.WriteString("\n\t")
b.WriteString(f.File)
b.WriteString(":")
b.WriteString(strconv.Itoa(f.Line))
b.WriteByte('\n')
if !more {
break
}
}
return b.String()
}
func addStackTrace(err error, skip int) error {
var e *StackTrace
if err != nil && errors.As(err, &e) {
return err
}
// construct an error to avoid infinite recursive loop
return &Error{
Message: "Stack trace",
wrapped: [2]error{NewStackTrace(1 + skip), err},
skip: 0,
}
}
// WithMsg returns an error wrapped by an [*Error] with a Message
func WithMsg(err error, msg string) error {
return New(OptMsg(msg), OptInner(err), OptSkip(1))
}
// WithKind returns an error wrapped by an [*Error] with a Kind and Message
func WithKind(err error, kind error, msg string) error {
return New(OptMsg(msg), OptKind(kind), OptInner(err), OptSkip(1))
}