-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging_test.go
More file actions
348 lines (300 loc) · 8.77 KB
/
Copy pathlogging_test.go
File metadata and controls
348 lines (300 loc) · 8.77 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package errors_test
import (
"bytes"
"context"
"encoding/json"
stderrors "errors"
"log/slog"
"testing"
"github.com/muonsoft/errors"
"github.com/muonsoft/errors/errorstest"
)
func TestAttrs_noError(t *testing.T) {
attrs := errors.Attrs(nil)
if attrs != nil {
t.Errorf("expected nil attrs for nil error, got %v", attrs)
}
}
func TestAttrs_errorWithoutStack(t *testing.T) {
err := errors.New("ooh")
attrs := errors.Attrs(err)
if len(attrs) != 0 {
t.Errorf("expected no attrs for error without fields, got %v", attrs)
}
}
func TestAttrs_errorWithStack(t *testing.T) {
err := errors.Wrap(
errors.Wrap(
errors.Errorf("ooh", errors.String("deepestKey", "deepestValue")),
errors.String("deepKey", "deepValue"),
),
errors.String("key", "value"),
)
attrs := errors.Attrs(err)
// Should have 3 attributes
if len(attrs) != 3 {
t.Fatalf("expected 3 attrs, got %d", len(attrs))
}
// Create a mock logger to use assertion helpers
logger := errorstest.NewLogger()
logger.Attrs = attrs
logger.AssertField(t, "key", "value")
logger.AssertField(t, "deepKey", "deepValue")
logger.AssertField(t, "deepestKey", "deepestValue")
}
func TestAttrs_joinedErrors(t *testing.T) {
err := errors.Wrap(
errors.Join(
errors.Wrap(
errors.Errorf("error 1", errors.String("key1", "value1")),
errors.String("key2", "value2"),
),
errors.Errorf("error 2", errors.String("key3", "value3")),
stderrors.Join(
errors.Errorf("error 3", errors.String("key4", "value4")),
errors.Errorf("error 4", errors.String("key5", "value5")),
),
),
)
attrs := errors.Attrs(err)
// Should have 5 attributes
if len(attrs) < 5 {
t.Fatalf("expected at least 5 attrs, got %d", len(attrs))
}
// Create a mock logger to use assertion helpers
logger := errorstest.NewLogger()
logger.Attrs = attrs
logger.AssertField(t, "key1", "value1")
logger.AssertField(t, "key2", "value2")
logger.AssertField(t, "key3", "value3")
logger.AssertField(t, "key4", "value4")
logger.AssertField(t, "key5", "value5")
}
func TestLog(t *testing.T) {
// Create a custom handler to capture log output
var capturedAttrs []slog.Attr
var capturedMsg string
var capturedLevel slog.Level
handler := &testHandler{
onHandle: func(ctx context.Context, r slog.Record) error {
capturedMsg = r.Message
capturedLevel = r.Level
r.Attrs(func(a slog.Attr) bool {
capturedAttrs = append(capturedAttrs, a)
return true
})
return nil
},
}
logger := slog.New(handler)
err := errors.Wrap(
errors.Errorf("test error", errors.String("user", "john"), errors.Int("id", 123)),
)
errors.Log(context.Background(), logger, err)
if capturedMsg != "test error" {
t.Errorf("expected message 'test error', got '%s'", capturedMsg)
}
if capturedLevel != slog.LevelError {
t.Errorf("expected level Error, got %v", capturedLevel)
}
if len(capturedAttrs) < 2 {
t.Fatalf("expected at least 2 attrs, got %d", len(capturedAttrs))
}
// Check for user and id attributes
hasUser := false
hasID := false
hasStackTrace := false
for _, attr := range capturedAttrs {
if attr.Key == "user" && attr.Value.String() == "john" {
hasUser = true
}
if attr.Key == "id" && attr.Value.Any() == int64(123) {
hasID = true
}
if attr.Key == "stackTrace" {
hasStackTrace = true
}
}
if !hasUser {
t.Error("expected 'user' attribute")
}
if !hasID {
t.Error("expected 'id' attribute")
}
if !hasStackTrace {
t.Error("expected 'stackTrace' attribute")
}
}
func TestLog_errorAttrIsTypedError(t *testing.T) {
orig := errors.Wrap(
errors.Errorf("typed error", errors.String("user", "john"), errors.Int("id", 123)),
)
attr, ok := lastTypedErrorAttr(captureLogAttrs(t, orig))
if !ok {
t.Fatal("expected typed 'error' attribute")
}
if attr.Value.Kind() == slog.KindLogValuer {
t.Fatal("expected 'error' attribute not to be KindLogValuer")
}
resolved, ok := attr.Value.Resolve().Any().(error)
if !ok {
t.Fatalf("expected Resolve().Any() to be error, got %T", attr.Value.Resolve().Any())
}
if _, isLogValuer := resolved.(slog.LogValuer); isLogValuer {
t.Fatal("expected wrapped log error not to implement slog.LogValuer")
}
if errors.Unwrap(resolved) != orig {
t.Fatal("expected Unwrap() to return the original error")
}
if !errors.Is(resolved, orig) {
t.Fatal("expected errors.Is to match the original error")
}
var tracer interface{ StackTrace() errors.StackTrace }
if !stderrors.As(resolved, &tracer) {
t.Fatal("expected StackTrace() on the unwrapped muonsoft error")
}
if len(tracer.StackTrace()) == 0 {
t.Fatal("expected non-empty stack trace")
}
}
func TestLog_errorAttrPreservesJoin(t *testing.T) {
err1 := errors.New("one")
err2 := errors.New("two")
joined := errors.Join(err1, err2)
attr, ok := lastTypedErrorAttr(captureLogAttrs(t, joined))
if !ok {
t.Fatal("expected typed 'error' attribute")
}
resolved, ok := attr.Value.Resolve().Any().(error)
if !ok {
t.Fatalf("expected Resolve().Any() to be error, got %T", attr.Value.Resolve().Any())
}
if !errors.Is(resolved, err1) || !errors.Is(resolved, err2) {
t.Fatal("expected Join chain to be preserved through Unwrap")
}
}
func TestLog_errorAttrAfterStringErrorField(t *testing.T) {
orig := errors.Wrap(errors.New("boom"), errors.String("error", "already used"))
attrs := captureLogAttrs(t, orig)
typed, ok := lastTypedErrorAttr(attrs)
if !ok {
t.Fatal("expected typed 'error' attribute after string 'error' attr")
}
if _, isError := typed.Value.Resolve().Any().(error); !isError {
t.Fatal("expected last 'error' attribute to type-assert to error")
}
hasStringError := false
for _, attr := range attrs {
if attr.Key == "error" && attr.Value.Kind() == slog.KindString {
hasStringError = true
break
}
}
if !hasStringError {
t.Fatal("expected original string 'error' attribute to remain")
}
}
func TestSlogAnyErrorIsLogValuer(t *testing.T) {
orig := errors.Wrap(errors.Errorf("wrap me"), errors.String("key", "value"))
attr := slog.Any("error", orig)
if attr.Value.Kind() != slog.KindLogValuer {
t.Fatalf("expected KindLogValuer, got %v", attr.Value.Kind())
}
resolved := attr.Value.Resolve()
if resolved.Kind() != slog.KindGroup {
t.Fatalf("expected resolved group, got %v", resolved.Kind())
}
if _, ok := resolved.Any().(error); ok {
t.Fatal("expected slog.Any without wrapper not to resolve to error")
}
}
func TestLog_jsonHandlerSerializesErrorAsString(t *testing.T) {
var buf bytes.Buffer
logger := slog.New(slog.NewJSONHandler(&buf, nil))
orig := errors.Wrap(errors.New("boom"), errors.String("user", "john"))
errors.Log(context.Background(), logger, orig)
var payload map[string]any
if err := json.Unmarshal(buf.Bytes(), &payload); err != nil {
t.Fatal(err)
}
errorField, ok := payload["error"].(string)
if !ok {
t.Fatalf("expected error to be a string, got %T (%v)", payload["error"], payload["error"])
}
if errorField != "boom" {
t.Errorf("expected error %q, got %q", "boom", errorField)
}
if payload["msg"] != "boom" {
t.Errorf("expected msg %q, got %v", "boom", payload["msg"])
}
}
func TestLogLevel(t *testing.T) {
for _, level := range []slog.Level{slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError} {
t.Run(level.String(), func(t *testing.T) {
var capturedLevel slog.Level
handler := &testHandler{
onHandle: func(ctx context.Context, r slog.Record) error {
capturedLevel = r.Level
return nil
},
}
logger := slog.New(handler)
err := errors.New("test")
errors.LogLevel(context.Background(), logger, level, err)
if capturedLevel != level {
t.Errorf("expected level %v, got %v", level, capturedLevel)
}
})
}
}
// testHandler is a simple slog.Handler for testing.
type testHandler struct {
onHandle func(ctx context.Context, r slog.Record) error
}
func (h *testHandler) Enabled(ctx context.Context, level slog.Level) bool {
return true
}
func (h *testHandler) Handle(ctx context.Context, r slog.Record) error {
if h.onHandle != nil {
return h.onHandle(ctx, r)
}
return nil
}
func (h *testHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return h
}
func (h *testHandler) WithGroup(name string) slog.Handler {
return h
}
func captureLogAttrs(t *testing.T, err error) []slog.Attr {
t.Helper()
var captured []slog.Attr
handler := &testHandler{
onHandle: func(ctx context.Context, r slog.Record) error {
r.Attrs(func(a slog.Attr) bool {
captured = append(captured, a)
return true
})
return nil
},
}
errors.Log(context.Background(), slog.New(handler), err)
if len(captured) == 0 {
t.Fatal("expected log attributes")
}
return captured
}
func lastTypedErrorAttr(attrs []slog.Attr) (slog.Attr, bool) {
var found slog.Attr
ok := false
for _, attr := range attrs {
if attr.Key != "error" {
continue
}
if _, isError := attr.Value.Resolve().Any().(error); isError {
found = attr
ok = true
}
}
return found, ok
}