-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
222 lines (203 loc) · 5.49 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
package serrors_test
import (
"errors"
"fmt"
"net"
"runtime"
"testing"
"github.com/Eun/serrors"
)
var _ error = &serrors.Error{} // make sure we implement the error interface
func testErrorFunc() error {
return serrors.New("deep error"). // [TestErrorWrapf10]
With("deep.key1", "value1").
With("deep.key2", "should be overwritten")
}
func TestError(t *testing.T) {
_, filename, _, ok := runtime.Caller(0)
Equal(t, true, ok)
t.Run("Errorf", func(t *testing.T) {
err := serrors.Errorf("some error"). // [TestErrorErrorf00]
With("key1", "value1").
With("key2", "value2")
NotNil(t, err)
Equal(t, "some error", err.Error())
expectedFields := map[string]any{
"key1": "value1",
"key2": "value2",
}
expectedStack := []serrors.ErrorStack{
{
ErrorMessage: "some error",
Fields: expectedFields,
StackTrace: []serrors.StackFrame{
buildStackFrameFromMarker(t, filename, "TestErrorErrorf00"),
},
},
}
Equal(t, expectedFields, serrors.GetFields(err))
CompareErrorStack(t, expectedStack, serrors.GetStack(err))
})
t.Run("Wrapf", func(t *testing.T) {
err := testErrorFunc() // [TestErrorWrapf11]
err = serrors.Wrapf(err, "some error"). // [TestErrorWrapf00] [TestErrorWrapf12]
With("deep.key2", "value2").
With("key1", "value1").
With("key2", "value2")
NotNil(t, err)
Equal(t, "some error: deep error", err.Error())
expectedFields := map[string]any{
"deep.key1": "value1",
"deep.key2": "value2",
"key1": "value1",
"key2": "value2",
}
expectedStack := []serrors.ErrorStack{
{
ErrorMessage: "some error",
Fields: map[string]any{
"deep.key2": "value2",
"key1": "value1",
"key2": "value2",
},
StackTrace: []serrors.StackFrame{
buildStackFrameFromMarker(t, filename, "TestErrorWrapf00"),
},
},
{
ErrorMessage: "deep error",
Fields: map[string]any{
"deep.key1": "value1",
"deep.key2": "should be overwritten",
},
StackTrace: []serrors.StackFrame{
buildStackFrameFromMarker(t, filename, "TestErrorWrapf10"),
buildStackFrameFromMarker(t, filename, "TestErrorWrapf11"),
},
},
}
Equal(t, expectedFields, serrors.GetFields(err))
CompareErrorStack(t, expectedStack, serrors.GetStack(err))
})
}
func TestGetFields(t *testing.T) {
testCases := []struct {
name string
error error
expectedFields map[string]any
}{
{
name: "error is nil",
error: nil,
expectedFields: nil,
},
{
name: "not an error",
error: errors.New("some error"),
expectedFields: nil,
},
{
name: "not containing fields",
error: serrors.New("some error"),
expectedFields: nil,
},
{
name: "regular",
error: serrors.New("").With("k", "v"),
expectedFields: map[string]any{"k": "v"},
},
{
name: "wrapped",
error: fmt.Errorf("some error: %w", serrors.New("some error").With("k", "v")),
expectedFields: map[string]any{"k": "v"},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
Equal(t, tc.expectedFields, serrors.GetFields(tc.error))
})
}
}
func TestGetFieldsAsArguments(t *testing.T) {
testCases := []struct {
name string
error error
expectedArguments []any
}{
{
name: "error is nil",
error: nil,
expectedArguments: nil,
},
{
name: "not an error",
error: errors.New("some error"),
expectedArguments: nil,
},
{
name: "not containing fields",
error: serrors.New("some error"),
expectedArguments: nil,
},
{
name: "regular",
error: serrors.New("").With("k", "v"),
expectedArguments: []any{"k", "v"},
},
{
name: "wrapped",
error: fmt.Errorf("some error: %w", serrors.New("some error").With("k", "v")),
expectedArguments: []any{"k", "v"},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
Equal(t, tc.expectedArguments, serrors.GetFieldsAsCombinedSlice(tc.error))
})
}
}
func TestUnwrap(t *testing.T) {
t.Run("wrapped error", func(t *testing.T) {
err1 := errors.New("error1")
err := serrors.Wrap(err1, "error2")
Equal(t, err1, errors.Unwrap(err))
})
t.Run("wrapped no error", func(t *testing.T) {
err := serrors.Wrap(nil, "error2")
Nil(t, errors.Unwrap(err))
})
}
func TestIs(t *testing.T) {
t.Run("wrapped error", func(t *testing.T) {
err := serrors.Wrap(net.ErrClosed, "error")
Equal(t, true, errors.Is(err, net.ErrClosed))
Equal(t, false, errors.Is(err, net.ErrWriteToConnected))
})
t.Run("wrapped no error", func(t *testing.T) {
err := serrors.Wrap(nil, "error")
Equal(t, false, errors.Is(err, net.ErrClosed))
Equal(t, false, errors.Is(err, net.ErrWriteToConnected))
})
}
func TestAs(t *testing.T) {
t.Run("wrapped error", func(t *testing.T) {
err := serrors.Wrap(&net.AddrError{Addr: "127.0.0.1"}, "error")
var cause1 *net.AddrError
Equal(t, true, errors.As(err, &cause1))
Equal(t, "127.0.0.1", cause1.Addr)
var cause2 *net.OpError
Equal(t, false, errors.As(err, &cause2))
Nil(t, cause2)
})
t.Run("wrapped no error", func(t *testing.T) {
err := serrors.Wrap(nil, "error")
var cause1 *net.AddrError
Equal(t, false, errors.As(err, &cause1))
Nil(t, cause1)
var cause2 *net.OpError
Equal(t, false, errors.As(err, &cause2))
Nil(t, cause2)
})
}