forked from go-errors/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_test.go
347 lines (274 loc) · 7.2 KB
/
error_test.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package errors
import (
"bytes"
"fmt"
"io"
"reflect"
"runtime"
"strings"
"testing"
)
func BenchmarkStackFormat(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
func() {
defer func() {
err := recover()
if err != 'a' {
b.Fatal(err)
}
e := Errorf("hi")
_ = string(e.Stack())
}()
a()
}()
}
}
func TestAs(t *testing.T) {
var errStrIn errorString = "TestForFun"
var errStrOut errorString
if As(errStrIn, &errStrOut) {
if errStrOut != "TestForFun" {
t.Errorf("direct errStr value is not returned")
}
} else {
t.Errorf("direct errStr is not returned")
}
errStrOut = ""
err := Wrap(errStrIn, 0)
if As(err, &errStrOut) {
if errStrOut != "TestForFun" {
t.Errorf("wrapped errStr value is not returned")
}
} else {
t.Errorf("wrapped errStr is not returned")
}
}
func TestStackFormat(t *testing.T) {
defer func() {
err := recover()
if err != 'a' {
t.Fatal(err)
}
e, expected := Errorf("hi"), callers()
bs := [][]uintptr{e.stack, expected}
if err := compareStacks(bs[0], bs[1]); err != nil {
t.Errorf("Stack didn't match")
t.Errorf(err.Error())
}
stack := string(e.Stack())
if !strings.Contains(stack, "a: b(5)") {
t.Errorf("Stack trace does not contain source line: 'a: b(5)'")
t.Errorf(stack)
}
if !strings.Contains(stack, "error_test.go:") {
t.Errorf("Stack trace does not contain file name: 'error_test.go:'")
t.Errorf(stack)
}
}()
a()
}
func TestSkipWorks(t *testing.T) {
defer func() {
err := recover()
if err != 'a' {
t.Fatal(err)
}
bs := [][]uintptr{Wrap("hi", 2).stack, callersSkip(2)}
if err := compareStacks(bs[0], bs[1]); err != nil {
t.Errorf("Stack didn't match")
t.Errorf(err.Error())
}
}()
a()
}
func TestNew(t *testing.T) {
err := New("foo")
if err.Error() != "foo" {
t.Errorf("Wrong message")
}
err = New(fmt.Errorf("foo"))
if err.Error() != "foo" {
t.Errorf("Wrong message")
}
bs := [][]uintptr{New("foo").stack, callers()}
if err := compareStacks(bs[0], bs[1]); err != nil {
t.Errorf("Stack didn't match")
t.Errorf(err.Error())
}
if err.ErrorStack() != err.TypeName()+" "+err.Error()+"\n"+string(err.Stack()) {
t.Errorf("ErrorStack is in the wrong format")
}
}
// This test should work for any go version
func TestIs(t *testing.T) {
if Is(nil, io.EOF) {
t.Errorf("nil is an error")
}
if !Is(io.EOF, io.EOF) {
t.Errorf("io.EOF is not io.EOF")
}
if !Is(io.EOF, New(io.EOF)) {
t.Errorf("io.EOF is not New(io.EOF)")
}
if !Is(New(io.EOF), New(io.EOF)) {
t.Errorf("New(io.EOF) is not New(io.EOF)")
}
if Is(io.EOF, fmt.Errorf("io.EOF")) {
t.Errorf("io.EOF is fmt.Errorf")
}
}
func TestWrapError(t *testing.T) {
e := func() error {
return Wrap("hi", 1)
}()
if e.Error() != "hi" {
t.Errorf("Constructor with a string failed")
}
if Wrap(fmt.Errorf("yo"), 0).Error() != "yo" {
t.Errorf("Constructor with an error failed")
}
if Wrap(e, 0) != e {
t.Errorf("Constructor with an Error failed")
}
if Wrap(nil, 0) != nil {
t.Errorf("Constructor with nil failed")
}
}
func TestWrapPrefixError(t *testing.T) {
e := func() error {
return WrapPrefix("hi", "prefix", 1)
}()
if e.Error() != "prefix: hi" {
t.Errorf("Constructor with a string failed")
}
if WrapPrefix(fmt.Errorf("yo"), "prefix", 0).Error() != "prefix: yo" {
t.Errorf("Constructor with an error failed")
}
prefixed := WrapPrefix(e, "prefix", 0)
original := e.(*Error)
if prefixed.Err != original.Err || !reflect.DeepEqual(prefixed.stack, original.stack) || !reflect.DeepEqual(prefixed.frames, original.frames) || prefixed.Error() != "prefix: prefix: hi" {
t.Errorf("Constructor with an Error failed")
}
if original.Error() == prefixed.Error() {
t.Errorf("WrapPrefix changed the original error")
}
if WrapPrefix(nil, "prefix", 0) != nil {
t.Errorf("Constructor with nil failed")
}
if !strings.HasSuffix(original.StackFrames()[0].File, "error_test.go") || strings.HasSuffix(original.StackFrames()[1].File, "error_test.go") {
t.Errorf("Skip failed")
}
}
func ExampleErrorf(x int) (int, error) {
if x%2 == 1 {
return 0, Errorf("can only halve even numbers, got %d", x)
}
return x / 2, nil
}
func ExampleWrapError() (error, error) {
// Wrap io.EOF with the current stack-trace and return it
return nil, Wrap(io.EOF, 0)
}
func ExampleWrapError_skip() {
defer func() {
if err := recover(); err != nil {
// skip 1 frame (the deferred function) and then return the wrapped err
err = Wrap(err, 1)
}
}()
}
func ExampleIs(reader io.Reader, buff []byte) {
_, err := reader.Read(buff)
if Is(err, io.EOF) {
return
}
}
func ExampleNew(UnexpectedEOF error) error {
// calling New attaches the current stacktrace to the existing UnexpectedEOF error
return New(UnexpectedEOF)
}
func ExampleWrap() error {
if err := recover(); err != nil {
return Wrap(err, 1)
}
return a()
}
func ExampleError_Error(err error) {
fmt.Println(err.Error())
}
func ExampleError_ErrorStack(err error) {
fmt.Println(err.(*Error).ErrorStack())
}
func ExampleError_Stack(err *Error) {
fmt.Println(err.Stack())
}
func ExampleError_TypeName(err *Error) {
fmt.Println(err.TypeName(), err.Error())
}
func ExampleError_StackFrames(err *Error) {
for _, frame := range err.StackFrames() {
fmt.Println(frame.File, frame.LineNumber, frame.Package, frame.Name)
}
}
func a() error {
b(5)
return nil
}
func b(i int) {
c()
}
func c() {
panic('a')
}
// compareStacks will compare a stack created using the errors package (actual)
// with a reference stack created with the callers function (expected). The
// first entry is not compared since the actual and expected stacks cannot
// be created at the exact same program counter position so the first entry
// will always differ somewhat. Returns nil if the stacks are equal enough and
// an error containing a detailed error message otherwise.
func compareStacks(actual, expected []uintptr) error {
if len(actual) != len(expected) {
return stackCompareError("Stacks does not have equal length", actual, expected)
}
for i, pc := range actual {
if i != 0 && pc != expected[i] {
return stackCompareError(fmt.Sprintf("Stacks does not match entry %d (and maybe others)", i), actual, expected)
}
}
return nil
}
func stackCompareError(msg string, actual, expected []uintptr) error {
return fmt.Errorf("%s\nActual stack trace:\n%s\nExpected stack trace:\n%s", msg, readableStackTrace(actual), readableStackTrace(expected))
}
func callers() []uintptr {
return callersSkip(1)
}
func callersSkip(skip int) []uintptr {
callers := make([]uintptr, MaxStackDepth)
length := runtime.Callers(skip+2, callers[:])
return callers[:length]
}
func readableStackTrace(callers []uintptr) string {
var result bytes.Buffer
frames := callersToFrames(callers)
for _, frame := range frames {
result.WriteString(fmt.Sprintf("%s:%d (%#x)\n\t%s\n", frame.File, frame.Line, frame.PC, frame.Function))
}
return result.String()
}
func callersToFrames(callers []uintptr) []runtime.Frame {
frames := make([]runtime.Frame, 0, len(callers))
framesPtr := runtime.CallersFrames(callers)
for {
frame, more := framesPtr.Next()
frames = append(frames, frame)
if !more {
return frames
}
}
}
type errorString string
func (e errorString) Error() string {
return string(e)
}