forked from go-errors/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
238 lines (180 loc) · 4.47 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
package errors
import (
"bytes"
"fmt"
"io"
"runtime/debug"
"testing"
)
func TestStackFormatMatches(t *testing.T) {
defer func() {
err := recover()
if err != 'a' {
t.Fatal(err)
}
bs := [][]byte{Errorf("hi").Stack(), debug.Stack()}
// Ignore the first line (as it contains the PC of the .Stack() call)
bs[0] = bytes.SplitN(bs[0], []byte("\n"), 2)[1]
bs[1] = bytes.SplitN(bs[1], []byte("\n"), 2)[1]
if bytes.Compare(bs[0], bs[1]) != 0 {
t.Errorf("Stack didn't match")
t.Errorf("%s", bs[0])
t.Errorf("%s", bs[1])
}
}()
a()
}
func TestSkipWorks(t *testing.T) {
defer func() {
err := recover()
if err != 'a' {
t.Fatal(err)
}
bs := [][]byte{Wrap("hi", 2).Stack(), debug.Stack()}
// should skip four lines of debug.Stack()
bs[1] = bytes.SplitN(bs[1], []byte("\n"), 5)[4]
if bytes.Compare(bs[0], bs[1]) != 0 {
t.Errorf("Stack didn't match")
t.Errorf("%s", bs[0])
t.Errorf("%s", bs[1])
}
}()
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 := [][]byte{New("foo").Stack(), debug.Stack()}
// Ignore the first line (as it contains the PC of the .Stack() call)
bs[0] = bytes.SplitN(bs[0], []byte("\n"), 2)[1]
bs[1] = bytes.SplitN(bs[1], []byte("\n"), 2)[1]
if bytes.Compare(bs[0], bs[1]) != 0 {
t.Errorf("Stack didn't match")
t.Errorf("%s", bs[0])
t.Errorf("%s", bs[1])
}
if err.ErrorStack() != err.TypeName()+" "+err.Error()+"\n"+string(err.Stack()) {
t.Errorf("ErrorStack is in the wrong format")
}
}
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).Error() != "<nil>" {
t.Errorf("Constructor with nil failed")
}
}
func TestWrapPrefixError(t *testing.T) {
e := func() error {
return WrapPrefix("hi", "prefix", 1)
}()
fmt.Println(e.Error())
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 || &prefixed.stack != &original.stack || &prefixed.frames != &original.frames || prefixed.Error() != "prefix: prefix: hi" {
t.Errorf("Constructor with an Error failed")
}
if WrapPrefix(nil, "prefix", 0).Error() != "prefix: <nil>" {
t.Errorf("Constructor with nil 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')
}