-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_test.go
289 lines (229 loc) · 7.77 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
package ctxd_test
import (
"bytes"
"context"
"encoding"
"encoding/json"
"errors"
"fmt"
"testing"
"github.com/bool64/ctxd"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/swaggest/usecase/status"
)
func TestWrap(t *testing.T) {
ctx := context.Background()
ctx = ctxd.AddFields(ctx, "country", "us")
var (
stringer fmt.Stringer
err = ctxd.WrapError(ctx, status.NotFound, "failed to find order", "id", 123)
)
assert.NotNil(t, err)
assert.Equal(t, "failed to find order: not found", err.Error())
assert.True(t, errors.As(err, &stringer))
assert.Equal(t, "failed to find order: not found, id: 123, country: us", stringer.String())
logOut := &bytes.Buffer{}
ctx = ctxd.WithLogWriter(ctx, logOut)
logger := ctxd.LoggerMock{}
ctxd.LogError(ctx, err, logger.Error)
var st status.Code
assert.True(t, errors.As(err, &st))
assert.Equal(t, st, status.NotFound)
assert.True(t, errors.Is(err, status.NotFound))
assert.False(t, errors.Is(err, status.Unknown))
assert.Equal(t,
`error: failed to find order: not found {"country":"us","id":123}`+"\n",
logOut.String())
}
func TestWrap_noKeys(t *testing.T) {
err := ctxd.WrapError(context.Background(), errors.New("failed"), "unable to can")
assert.NotNil(t, err)
assert.Equal(t, "unable to can: failed", err.Error())
}
func TestWrap_nilErr(t *testing.T) {
err := ctxd.WrapError(context.Background(), nil, "failed to win")
assert.Nil(t, err)
}
func TestWrap_noCtxKeys(t *testing.T) {
var (
stringer fmt.Stringer
err = ctxd.WrapError(context.Background(), errors.New("failed"), "unable to can",
"key1", 123,
"key2", "abc",
)
)
assert.NotNil(t, err)
assert.True(t, errors.As(err, &stringer))
assert.Equal(t, "unable to can: failed, key1: 123, key2: abc", stringer.String())
}
func TestWrap_doubleWrap(t *testing.T) {
ctx := context.Background()
err := ctxd.WrapError(ctx, status.NotFound, "failed to find order", "id", 123)
ctxd.LogError(ctx, err, func(ctx context.Context, msg string, keysAndValues ...interface{}) {
assert.Equal(t, "failed to find order: not found", msg)
assert.Equal(t, []interface{}{"id", 123}, keysAndValues)
})
ctx = ctxd.AddFields(ctx, "extra", 321)
err = ctxd.WrapError(ctx, err, "wrapped")
ctxd.LogError(ctx, err, func(ctx context.Context, msg string, keysAndValues ...interface{}) {
assert.Equal(t, "wrapped: failed to find order: not found", msg)
assert.Equal(t, []interface{}{"id", 123, "extra", 321}, keysAndValues)
})
}
func TestNew(t *testing.T) {
var (
stringer fmt.Stringer
err = ctxd.NewError(context.Background(), "failed",
"key1", 123,
"key2", "abc",
)
)
assert.NotNil(t, err)
assert.True(t, errors.As(err, &stringer))
assert.Equal(t, "failed, key1: 123, key2: abc", stringer.String())
}
func TestNew_noFields(t *testing.T) {
err := ctxd.NewError(context.Background(), "failed")
assert.NotNil(t, err)
assert.Equal(t, "failed", err.Error())
}
func TestNew_malformedFields(t *testing.T) {
var (
stringer fmt.Stringer
err = ctxd.NewError(context.Background(), "failed",
"key1", 1,
123, 2, // non-string key is breaking processing
"key3", 3,
)
)
assert.NotNil(t, err)
assert.True(t, errors.As(err, &stringer))
assert.Equal(t, "failed, key1: 1, malformed fields: [123 2 key3 3]", stringer.String())
}
func TestStructuredError_Fields(t *testing.T) {
var (
se ctxd.StructuredError
err = ctxd.NewError(context.Background(), "failed",
"key1", 1,
123, 2, // Non-string key is breaking processing.
"key3", 3,
)
)
assert.NotNil(t, err)
assert.True(t, errors.As(err, &se))
assert.Equal(t, map[string]interface{}{
"key1": 1,
"malformedFields": []interface{}{
123, 2, // Non-string key is breaking processing.
"key3", 3,
},
}, se.Fields())
j, jerr := json.Marshal(err)
require.NoError(t, jerr)
assert.Equal(t, `"failed"`, string(j))
assert.Equal(t, `failed`, fmt.Sprintf("%s", err))
var tm encoding.TextMarshaler
assert.True(t, errors.As(err, &tm))
m, merr := tm.MarshalText()
require.NoError(t, merr)
assert.Equal(t, `failed`, string(m))
}
func TestLog(t *testing.T) {
ctx := context.Background()
logOut := &bytes.Buffer{}
ctx = ctxd.WithLogWriter(ctx, logOut)
logger := ctxd.LoggerMock{}
err := ctxd.NewError(ctx, "failed", "id", 123)
assert.NotNil(t, err)
ctxd.LogError(ctx, err, logger.Error)
ctxd.LogError(ctx, nil, logger.Error)
ctxd.LogError(ctx, ctxd.NewError(ctx, "failed with no fields"), logger.Error)
assert.Equal(t,
`error: failed {"id":123}
error: failed with no fields null
`,
logOut.String())
}
func BenchmarkStructuredError_Error(b *testing.B) {
ctx := context.Background()
ctx = ctxd.AddFields(ctx, "country", "us")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
err := ctxd.WrapError(ctx, status.NotFound, "failed to find order", "id", 123)
if err != nil {
_ = err.Error()
} else {
b.Fail()
}
}
}
func BenchmarkFmt_Errorf(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
err := fmt.Errorf("failed to find item, country: %s, id: %d: %w", "us", 123, status.NotFound)
_ = err.Error()
}
}
func BenchmarkWrapError(b *testing.B) {
ctx := context.Background()
ctx = ctxd.AddFields(ctx, "country", "us")
logFunc := func(ctx context.Context, msg string, keysAndValues ...interface{}) {}
e1 := ctxd.NewError(ctx, "not found", "a", 1)
b.ReportAllocs()
var err error
for i := 0; i < b.N; i++ {
err = ctxd.WrapError(ctx, e1, "failed to find order", "id", 123)
}
ctxd.LogError(ctx, err, logFunc)
}
func TestSentinelError_Error(t *testing.T) {
assert.EqualError(t, ctxd.SentinelError("failed"), "failed")
}
func TestLabeledError(t *testing.T) {
err1 := errors.New("failed")
label1 := ctxd.SentinelError("miserably")
label2 := ctxd.SentinelError("hopelessly")
err := ctxd.LabeledError(fmt.Errorf("oops: %w", err1), label1, label2)
assert.True(t, errors.Is(err, err1))
assert.True(t, errors.Is(err, label1))
assert.True(t, errors.Is(err, label2))
// Labels do not implicitly contribute to error message.
assert.Equal(t, "oops: failed", err.Error())
// If there are two matches, only first is returned.
var se ctxd.SentinelError
assert.True(t, errors.As(err, &se))
assert.Equal(t, "miserably", string(se))
}
func TestTuples_Fields(t *testing.T) {
assert.Equal(t, map[string]interface{}(nil), ctxd.Tuples(nil).Fields()) // Empty tuples.
assert.Equal(t, map[string]interface{}{"malformedFields": []interface{}{1, 2}},
ctxd.Tuples{1, 2}.Fields()) // String key expected.
assert.Equal(t, map[string]interface{}{"malformedFields": []interface{}{"key"}},
ctxd.Tuples{"key"}.Fields()) // Key without a value.
assert.Equal(t, map[string]interface{}{"malformedFields": []interface{}{"", 123}},
ctxd.Tuples{"", 123}.Fields()) // Empty key.
assert.Equal(t, map[string]interface{}{"a": 123, "b": 456},
ctxd.Tuples{"a", 123, "b", 456}.Fields()) // All good.
}
func TestMultiError(t *testing.T) {
errPrimary := errors.New("failed")
errSecondary1 := ctxd.SentinelError("miserably")
errSecondary2 := ctxd.SentinelError("hopelessly")
errSecondary3 := ctxd.SentinelError("deadly")
err := ctxd.MultiError(fmt.Errorf("oops: %w", errPrimary), errSecondary1, errSecondary2)
assert.True(t, errors.Is(err, errPrimary))
assert.True(t, errors.Is(err, errSecondary1))
assert.True(t, errors.Is(err, errSecondary2))
assert.False(t, errors.Is(err, errSecondary3))
// Labels do not implicitly contribute to error message.
assert.Equal(t, "oops: failed", err.Error())
// If there are two matches, only first is returned.
var errSentinel ctxd.SentinelError
assert.True(t, errors.As(err, &errSentinel))
assert.Equal(t, "miserably", string(errSentinel))
}
func TestMultiError_invalid(t *testing.T) {
assert.Equal(t, "empty multi error", ctxd.MultiError(nil).Error())
assert.Equal(t, "secondary fail", ctxd.MultiError(nil, errors.New("secondary fail")).Error())
}