-
Notifications
You must be signed in to change notification settings - Fork 1
/
fields_test.go
256 lines (218 loc) · 8.33 KB
/
fields_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
package errors_test
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/mailgun/errors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestToMapToLogrusFindsLastStackTrace(t *testing.T) {
err := errors.New("this is an error")
// --- Should report this line number for the stack in the chain ---
err = errors.Wrap(err, "last")
// ----------------------------------
err = errors.Wrap(err, "second")
err = errors.Wrap(err, "first")
t.Run("ToMap() finds the last stack in the chain", func(t *testing.T) {
m := errors.ToMap(err)
assert.NotNil(t, m)
assert.Equal(t, 21, m["excLineNum"])
})
t.Run("ToLogrus() finds the last stack in the chain", func(t *testing.T) {
f := errors.ToLogrus(err)
require.NotNil(t, f)
b := bytes.Buffer{}
logrus.SetOutput(&b)
logrus.WithFields(f).Info("test logrus fields")
logrus.SetOutput(os.Stdout)
fmt.Printf("%s\n", b.String())
assert.Contains(t, b.String(), "excLineNum=21")
})
}
func TestFields(t *testing.T) {
err := &ErrTest{Msg: "query error"}
wrap := errors.Fields{"key1": "value1"}.Wrap(err, "message")
assert.NotNil(t, wrap)
t.Run("Unwrap should return ErrTest", func(t *testing.T) {
u := errors.Unwrap(wrap)
require.NotNil(t, u)
assert.Equal(t, "query error", u.Error())
})
t.Run("Extract fields as a normal map", func(t *testing.T) {
m := errors.ToMap(wrap)
require.NotNil(t, m)
assert.Equal(t, "value1", m["key1"])
assert.Regexp(t, ".*/fields_test.go", m["excFileName"])
assert.Regexp(t, "\\d*", m["excLineNum"])
assert.Equal(t, "message: query error", m["excValue"])
assert.Equal(t, "errors_test.TestFields", m["excFuncName"])
assert.Equal(t, "*errors_test.ErrTest", m["excType"])
assert.Len(t, m, 6)
})
t.Run("Can use errors.Is() from std `errors` package", func(t *testing.T) {
assert.True(t, errors.Is(err, &ErrTest{}))
assert.True(t, errors.Is(wrap, &ErrTest{}))
})
t.Run("Can use errors.As() from std `errors` package", func(t *testing.T) {
myErr := &ErrTest{}
assert.True(t, errors.As(wrap, &myErr))
assert.Equal(t, myErr.Msg, "query error")
})
t.Run("Extract as Logrus fields", func(t *testing.T) {
f := errors.ToLogrus(wrap)
require.NotNil(t, f)
b := bytes.Buffer{}
logrus.SetOutput(&b)
logrus.WithFields(f).Info("test logrus fields")
logrus.SetOutput(os.Stdout)
assert.Contains(t, b.String(), "test logrus fields")
assert.Contains(t, b.String(), `excValue="message: query error"`)
assert.Contains(t, b.String(), `excType="*errors_test.ErrTest"`)
assert.Contains(t, b.String(), "key1=value1")
assert.Contains(t, b.String(), "excFuncName=errors_test.TestFields")
assert.Regexp(t, "excFileName=.*/fields_test.go", b.String())
assert.Regexp(t, "excLineNum=\\d*", b.String())
// OUTPUT: time="2023-01-26T10:37:48-05:00" level=info msg="test logrus fields"
// excFileName=errors/fields_test.go excFuncName=errors_test.TestFields
// excLineNum=18 excType="*errors_test.ErrTest" excValue="message: query error" key1=value1
// t.Log(b.String())
assert.Equal(t, "message: query error", wrap.Error())
out := fmt.Sprintf("%+v", wrap)
assert.True(t, strings.Contains(out, `message: query error (key1=value1)`))
})
t.Run("ToLogrus() should extract the error with StackTrace() from the chain", func(t *testing.T) {
// This error has no StackTrace() method
err := fmt.Errorf("I have no stack trace: %w", wrap)
// ToLogrus() should find the wrapped error in the chain that has the StackTrace() method.
f := errors.ToLogrus(err)
// t.Log(f)
assert.Regexp(t, ".*/fields_test.go", f["excFileName"])
assert.Regexp(t, "\\d*", f["excLineNum"])
assert.Equal(t, "I have no stack trace: message: query error", f["excValue"])
assert.Equal(t, "errors_test.TestFields", f["excFuncName"])
assert.Equal(t, "*errors.fields", f["excType"])
assert.Equal(t, "value1", f["key1"])
assert.Len(t, f, 6)
require.NotNil(t, f)
})
t.Run("Wrap() should return nil, if error is nil", func(t *testing.T) {
got := errors.Fields{"some": "context"}.Wrap(nil, "no error")
assert.Nil(t, got)
})
t.Run("Wrapf() should return nil, if error is nil", func(t *testing.T) {
got := errors.Fields{"some": "context"}.Wrapf(nil, "no '%d' error", 1)
assert.Nil(t, got)
})
}
func TestErrorf(t *testing.T) {
err := errors.New("this is an error")
wrap := errors.Fields{"key1": "value1", "key2": "value2"}.Wrap(err, "message")
err = fmt.Errorf("wrapped: %w", wrap)
assert.Equal(t, fmt.Sprintf("%s", err), "wrapped: message: this is an error")
}
func TestNestedFields(t *testing.T) {
err := errors.New("this is an error")
err = errors.Fields{"key1": "value1"}.Wrap(err, "message")
err = errors.Wrap(err, "second")
err = errors.Fields{"key2": "value2"}.Wrap(err, "message")
err = errors.Wrap(err, "first")
t.Run("ToMap() collects all values from nested fields", func(t *testing.T) {
m := errors.ToMap(err)
assert.NotNil(t, m)
assert.Equal(t, "value1", m["key1"])
assert.Equal(t, "value2", m["key2"])
})
t.Run("ToLogrus() collects all values from nested fields", func(t *testing.T) {
f := errors.ToLogrus(err)
require.NotNil(t, f)
b := bytes.Buffer{}
logrus.SetOutput(&b)
logrus.WithFields(f).Info("test logrus fields")
logrus.SetOutput(os.Stdout)
assert.Contains(t, b.String(), "test logrus fields")
assert.Contains(t, b.String(), "key1=value1")
assert.Contains(t, b.String(), "key2=value2")
})
}
func TestFieldsFmtDirectives(t *testing.T) {
t.Run("Wrap() with a message", func(t *testing.T) {
err := errors.Fields{"key1": "value1"}.Wrap(errors.New("error"), "shit happened")
assert.Equal(t, "shit happened: error", fmt.Sprintf("%s", err))
assert.Equal(t, "shit happened: error", fmt.Sprintf("%v", err))
assert.Equal(t, "shit happened: error (key1=value1)", fmt.Sprintf("%+v", err))
assert.Equal(t, "*errors.fields", fmt.Sprintf("%T", err))
})
t.Run("Wrap() without a message", func(t *testing.T) {
err := errors.Fields{"key1": "value1"}.Wrap(errors.New("error"), "")
assert.Equal(t, "error", fmt.Sprintf("%s", err))
assert.Equal(t, "error", fmt.Sprintf("%v", err))
assert.Equal(t, "error (key1=value1)", fmt.Sprintf("%+v", err))
assert.Equal(t, "*errors.fields", fmt.Sprintf("%T", err))
})
}
func TestFieldsErrorValue(t *testing.T) {
err := io.EOF
wrap := errors.Fields{"key1": "value1"}.Wrap(err, "message")
assert.True(t, errors.Is(wrap, io.EOF))
}
func TestHasFields(t *testing.T) {
hf := &ErrHasFields{M: "error", F: map[string]any{"file": "errors.go"}}
err := errors.Fields{"key1": "value1"}.Wrap(hf, "")
m := errors.ToMap(err)
require.NotNil(t, m)
assert.Equal(t, "value1", m["key1"])
assert.Equal(t, "errors.go", m["file"])
}
func TestWrapFields(t *testing.T) {
err := errors.New("last")
err = errors.Wrap(err, "second")
err = errors.WrapFields(err, errors.Fields{"key1": "value1"}, "fields")
err = errors.Wrap(err, "first")
m := errors.ToMap(err)
require.NotNil(t, m)
assert.Equal(t, "value1", m["key1"])
assert.Equal(t, "first: fields: second: last", err.Error())
}
func TestWrapFieldsf(t *testing.T) {
err := errors.New("last")
err = errors.Wrap(err, "second")
err = errors.WrapFieldsf(err, errors.Fields{"key1": "value1"}, "fields '%d'", 42)
err = errors.Wrap(err, "first")
m := errors.ToMap(err)
require.NotNil(t, m)
assert.Equal(t, "value1", m["key1"])
assert.Equal(t, "first: fields '42': second: last", err.Error())
}
func TestFieldsError(t *testing.T) {
t.Run("Fields.Error() should create a new error", func(t *testing.T) {
err := errors.Fields{"key1": "value1"}.Error("error")
m := errors.ToMap(err)
require.NotNil(t, m)
assert.Equal(t, "value1", m["key1"])
assert.Equal(t, "error", err.Error())
})
t.Run("Fields.Errorf() should create a new error", func(t *testing.T) {
err := errors.Fields{"key1": "value1"}.Errorf("error '%d'", 1)
m := errors.ToMap(err)
require.NotNil(t, m)
assert.Equal(t, "value1", m["key1"])
assert.Equal(t, "error '1'", err.Error())
})
}
func TestFieldsStack(t *testing.T) {
err := errors.Fields{"key1": "value1"}.Stack(io.EOF)
m := errors.ToMap(err)
require.NotNil(t, m)
assert.Equal(t, "value1", m["key1"])
assert.Equal(t, io.EOF.Error(), err.Error())
}
// Ensure errors.Fields returns an error that is compatible with `github.com/pkf/errors.Cause()`
func TestFieldsCause(t *testing.T) {
err := errors.Fields{"key1": "value1"}.Wrap(io.EOF, "message")
assert.Equal(t, io.EOF, pkgErrorCause(err))
}