-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin.go
More file actions
258 lines (216 loc) · 5.1 KB
/
builtin.go
File metadata and controls
258 lines (216 loc) · 5.1 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
258
package errors
import (
"errors"
"fmt"
"strings"
)
var (
// SkipRuntimeStackTrace is a flag to skip the runtime stack trace
//
// It is useful to skip the runtime stack trace when you want to get the error message
// without the runtime stack trace
//
// It is true by default
SkipRuntimeStackTrace = true
)
// Is represents builtin errors.Is
func Is(err, target error) bool {
return errors.Is(Unwrap(err), Unwrap(target))
}
// As represents builtin errors.As
func As(err error, target any) bool {
return errors.As(err, target)
}
// Unwrap represents builtin errors.Unwrap
func Unwrap(err error) error {
if err == nil {
return nil
}
u, ok := err.(unwrap)
if ok {
return u.Unwrap()
}
return err
}
type errorString struct {
message string
}
func (e errorString) Error() string {
return e.message
}
// New creates a new error with stack trace
func New(text string) Error {
return newError(text, 1)
}
// Wrap wraps an error and formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string.
//
// # It has better performance than Errorf with '%w' verb
//
// is the same as
//
// errors.Errorf("%w", err)
func Wrap(err error, args ...any) Error {
var message string
if len(args) != 0 {
message = fmt.Sprint(args...)
}
return wrap(err, message, 1, false)
}
// Wrapf wraps an error and formats according to a format specifier and returns the resulting string.
//
// # It has better performance than Errorf with '%w' verb
//
// is the same as
//
// errors.Errorf("%w", err)
func Wrapf(err error, format string, args ...any) Error {
if len(args) != 0 {
return wrap(err, fmt.Sprintf(format, args...), 1, false)
}
return wrap(err, format, 1, false)
}
// Errorf creates a formatted error, supporting '%w' verb for error wrapping
func Errorf(format string, args ...any) Error {
return errorf(NewTemplate(), format, args...)
}
func errorf(template Template, format string, args ...any) Error {
if len(args) == 0 {
return newError(format, 2, template)
}
// Check if args contains error types and format string contains %w
if strings.Contains(format, "%w") {
before, _, _ := strings.Cut(format, "%w")
idx := strings.Count(before, "%")
if len(args) < idx {
return New("errors: Errorf format contains more than one '%w' verb")
}
if err, ok := args[idx].(error); ok {
// Replace %w with %v and don't pass error to formatting arguments
newFormat := strings.Replace(format, "%w", "%s", 1)
format = newFormat
if err == nil {
return newError(fmt.Sprintf(format, args...), 2, template)
} else {
return wrap(err, fmt.Sprintf(format, args...), 2, true, template)
}
} else {
return nil
}
}
return newError(fmt.Sprintf(format, args...), 2, template)
}
func replaceFormatError(format string, args ...any) string {
var (
buf = []byte{}
p = 0
)
b := stringBuilderPool.Get().(*strings.Builder)
defer stringBuilderPool.Put(b)
b.Reset()
b.Grow(len(format))
for i, char := range format {
if p > len(args) {
_, _ = b.WriteRune(char)
continue
}
switch char {
case '%':
if len(buf) == 1 && buf[0] == '%' {
_ = b.WriteByte('%')
_ = b.WriteByte('%')
buf = []byte{}
continue
}
if len(buf) != 0 {
_, _ = b.Write(buf)
p++
}
buf = []byte{}
buf = append(buf, format[i])
default:
if len(buf) == 0 {
_, _ = b.WriteRune(char)
continue
}
buf = append(buf, format[i])
}
if len(buf) > 3 {
continue
}
switch string(buf) {
case "%v", "%+v", "%#v", "%+#v", "%#+v":
if _, ok := args[p].(Error); ok {
buf = []byte{'%', 's'}
}
}
}
if len(buf) != 0 {
_, _ = b.Write(buf)
}
return b.String()
}
func newError(text string, ignoreCallStackCount int, tp ...Template) Error {
stack := getStack(ignoreCallStackCount)
lastCaller := frame{}
if len(stack) != 0 {
lastCaller = stack[0]
}
template := NewTemplate()
if len(tp) != 0 {
template = tp[0]
}
return &errorStack{
message: text,
cause: errorString{message: text},
lastCaller: lastCaller,
stack: stack,
attr: template.Attrs(lastCaller),
}
}
func wrap(err error, message string, ignoreCallStackCount int, combineStack bool, tp ...Template) Error {
if err == nil {
return nil
}
var (
msg string
attrs []attr
tempAttrs []attr
lastCaller frame
cause = err
stack = getStack(ignoreCallStackCount)
ignore = combineStack
template = NewTemplate()
)
if len(stack) != 0 {
lastCaller = stack[0]
}
if len(tp) != 0 {
template = tp[0]
tempAttrs = template.Attrs(lastCaller)
}
if message == "" {
msg = err.Error()
} else {
if ignore {
msg = message
} else {
msg = message + ", err: " + err.Error()
}
}
if err, ok := err.(*errorStack); ok {
cause = err.cause
attrs = make([]attr, 0, len(err.attr)+len(tempAttrs))
attrs = append(attrs, err.attr...)
if len(err.stack) > len(stack) {
stack = err.stack[:]
}
}
attrs = append(attrs, tempAttrs...)
return &errorStack{
message: msg,
cause: cause,
lastCaller: lastCaller,
stack: stack[:],
attr: attrs,
}
}