-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors_test.go
More file actions
143 lines (121 loc) · 3.93 KB
/
errors_test.go
File metadata and controls
143 lines (121 loc) · 3.93 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
package errors_test
import (
"fmt"
"testing"
stderr "errors"
"github.com/mailgun/errors"
"github.com/mailgun/errors/callstack"
"github.com/stretchr/testify/assert"
)
// Ensure errors.Is remains stdlib compliant
func TestIs(t *testing.T) {
err := errors.New("bottom")
top := fmt.Errorf("top: %w", err)
assert.True(t, stderr.Is(top, err))
assert.True(t, errors.Is(top, err))
}
// Ensure errors.As remains stdlib compliant
func TestAs(t *testing.T) {
err := &ErrTest{Msg: "bottom"}
top := fmt.Errorf("top: %w", err)
var exp *ErrTest
assert.True(t, stderr.As(top, &exp))
assert.True(t, errors.As(top, &exp))
}
func TestLast(t *testing.T) {
err := errors.New("bottom")
err = errors.Wrap(err, "last")
err = errors.Wrap(err, "second")
err = errors.Wrap(err, "first")
err = errors.Errorf("wrapped: %w", err)
// errors.As() returns the "first" error in the chain with a stack trace
var first callstack.HasStackTrace
assert.True(t, errors.As(err, &first))
assert.Equal(t, "first: second: last: bottom", first.(error).Error())
// errors.Last() returns the last error in the chain with a stack trace
var last callstack.HasStackTrace
assert.True(t, errors.Last(err, &last))
assert.Equal(t, "last: bottom", last.(error).Error())
// If no stack trace is found, then should not set target and should return false
assert.False(t, errors.Last(errors.New("no stack"), &last))
assert.Equal(t, "last: bottom", last.(error).Error())
}
type ErrTest struct {
Msg string
}
func (e *ErrTest) Error() string {
return e.Msg
}
func (e *ErrTest) Is(target error) bool {
_, ok := target.(*ErrTest)
return ok
}
type ErrHasFields struct {
M string
F map[string]any
}
func (e *ErrHasFields) Error() string {
return e.M
}
func (e *ErrHasFields) Is(target error) bool {
_, ok := target.(*ErrHasFields)
return ok
}
func (e *ErrHasFields) HasFields() map[string]any {
return e.F
}
// Benchmarks for comparison purposes
/*
go test -bench=. -benchmem -count=5 -run="^$" ./...
goos: darwin
goarch: arm64
pkg: github.com/mailgun/errors
cpu: Apple M3 Pro
BenchmarkErrors-12 4825278 221.8 ns/op 328 B/op 3 allocs/op
BenchmarkErrors-12 5548051 216.4 ns/op 328 B/op 3 allocs/op
BenchmarkErrors-12 5548090 215.5 ns/op 328 B/op 3 allocs/op
BenchmarkErrors-12 5548306 215.7 ns/op 328 B/op 3 allocs/op
BenchmarkErrors-12 5557860 215.8 ns/op 328 B/op 3 allocs/op
*/
func BenchmarkErrorsWrap(b *testing.B) {
base := errors.New("init")
for b.Loop() {
errors.Wrap(base, "loop")
}
}
/*
go test -bench=. -benchmem -count=5 -run="^$" ./...
goos: darwin
goarch: arm64
pkg: github.com/mailgun/errors
cpu: Apple M3 Pro
BenchmarkErrorsWrapf-12 4733302 252.8 ns/op 336 B/op 4 allocs/op
BenchmarkErrorsWrapf-12 4750388 252.1 ns/op 336 B/op 4 allocs/op
BenchmarkErrorsWrapf-12 4720851 251.7 ns/op 336 B/op 4 allocs/op
BenchmarkErrorsWrapf-12 4740823 252.3 ns/op 336 B/op 4 allocs/op
BenchmarkErrorsWrapf-12 4753670 254.1 ns/op 336 B/op 4 allocs/op
*/
func BenchmarkErrorsWrapf(b *testing.B) {
base := errors.New("init")
for b.Loop() {
errors.Wrapf(base, "loop %s", "two")
}
}
/*
go test -bench=. -benchmem -count=5 -run="^$" ./...
goos: darwin
goarch: arm64
pkg: github.com/mailgun/errors
cpu: Apple M3 Pro
BenchmarkErrorsStack-12 5713897 210.4 ns/op 304 B/op 3 allocs/op
BenchmarkErrorsStack-12 5677599 210.1 ns/op 304 B/op 3 allocs/op
BenchmarkErrorsStack-12 5701461 210.1 ns/op 304 B/op 3 allocs/op
BenchmarkErrorsStack-12 5655940 210.1 ns/op 304 B/op 3 allocs/op
BenchmarkErrorsStack-12 5574022 210.8 ns/op 304 B/op 3 allocs/op
*/
func BenchmarkErrorsStack(b *testing.B) {
base := errors.New("init")
for b.Loop() {
_ = errors.Stack(base)
}
}