-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattrs_test.go
188 lines (150 loc) · 5.94 KB
/
attrs_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
package errors_test
import (
"bytes"
"context"
"fmt"
"github.com/kapetan-io/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"log/slog"
"testing"
)
// NOTE: Tests are sensitive to line changes, only add new tests to the end of this file
func TestAttrsWithCodeLoc(t *testing.T) {
var w bytes.Buffer
log := slog.New(slog.NewTextHandler(&w, &slog.HandlerOptions{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
}}))
err := errors.With("foo", "bar").Error("this is an error")
require.Error(t, err)
log.LogAttrs(context.Background(), slog.LevelInfo, err.Error(), errors.AttrsFromWithCodeLoc(err)...)
assert.Contains(t, w.String(), "msg=\"this is an error\"")
assert.Contains(t, w.String(), "foo=bar")
assert.Contains(t, w.String(), "code.filepath=")
assert.Contains(t, w.String(), "errors/attrs_test.go")
assert.Contains(t, w.String(), "code.function=github.com/kapetan-io/errors_test.TestAttrsWithCodeLoc")
assert.Contains(t, w.String(), "code.lineno=26")
w.Reset()
err = errors.With(slog.String("friendship", "magic")).Errorf("wrapping the previous: %w", err)
require.Error(t, err)
log.LogAttrs(context.Background(), slog.LevelInfo, err.Error(), errors.AttrsFromWithCodeLoc(err)...)
assert.Contains(t, w.String(), "msg=\"wrapping the previous: this is an error\"")
assert.Contains(t, w.String(), "foo=bar")
assert.Contains(t, w.String(), "friendship=magic")
assert.Contains(t, w.String(), "code.filepath=")
assert.Contains(t, w.String(), "errors/attrs_test.go")
assert.Contains(t, w.String(), "code.function=github.com/kapetan-io/errors_test.TestAttrsWithCodeLoc")
assert.Contains(t, w.String(), "code.lineno=26")
//t.Log(buf.String())
// level=INFO msg="wrapping the previous: this is an error"
// friendship=magic
// foo=bar
// code.filepath=/Users/thrawn/Development/errors/attrs_test.go
// code.function=github.com/kapetan-io/errors_test.TestAttrsWithCodeLoc
// code.lineno=26
}
func TestAttrs(t *testing.T) {
err := errors.New("query error")
wrap := errors.With("key1", "value1").Errorf("message: %w", err)
assert.NotNil(t, wrap)
t.Run("UnwrapReturnsWrappedError", func(t *testing.T) {
u := errors.Unwrap(wrap)
require.NotNil(t, u)
assert.Equal(t, "query error", u.Error())
})
t.Run("ExtractAttrs", func(t *testing.T) {
as := errors.AttrsFrom(wrap)
require.NotNil(t, as)
assert.True(t, as[0].Equal(slog.Any("key1", "value1")))
assert.Len(t, as, 1)
})
t.Run("IsFromStdErrorsPackage", func(t *testing.T) {
again := fmt.Errorf("wrap again: %w", wrap)
assert.True(t, errors.Is(again, &errors.ErrAttrs{}))
})
t.Run("AsFromStdErrorsPackage", func(t *testing.T) {
myErr := &errors.ErrAttrs{}
again := fmt.Errorf("wrap again: %w", wrap)
assert.True(t, errors.As(again, &myErr))
assert.Equal(t, "message: query error", myErr.Error())
})
t.Run("AsWithHasAttrs", func(t *testing.T) {
var a errors.HasAttrs
require.True(t, errors.As(wrap, &a))
require.NotNil(t, a)
b := bytes.Buffer{}
log := slog.New(slog.NewTextHandler(&b, nil))
log.LogAttrs(context.Background(), slog.LevelError, "test log attrs", errors.AttrsFrom(a)...)
assert.Contains(t, b.String(), "test log attrs")
assert.Contains(t, b.String(), "key1=value1")
})
t.Run("WrapShouldReturnNilIfErrorIsNil", func(t *testing.T) {
got := errors.With("some", "context").Wrap(nil)
assert.Nil(t, got)
})
t.Run("FormatTest", func(t *testing.T) {
assert.Equal(t, "message: query error", wrap.Error())
// %s
assert.Equal(t, `message: query error`, fmt.Sprintf("%s", wrap))
// %v
assert.Equal(t, `message: query error`, fmt.Sprintf("%v", wrap))
// %+v
assert.Equal(t, `message: query error (key1=value1)`, fmt.Sprintf("%+v", wrap))
})
t.Run("With", func(t *testing.T) {
err := errors.New("query error")
wrap := errors.WithAttr(slog.String("key", "value")).Errorf("message: %w", err)
assert.NotNil(t, wrap)
assert.Equal(t, `message: query error (key=value)`, fmt.Sprintf("%+v", wrap))
})
}
func TestAttrsAll(t *testing.T) {
var w bytes.Buffer
log := slog.New(slog.NewTextHandler(&w, &slog.HandlerOptions{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
}}))
err := errors.With("foo", "bar").Error("this is an error")
require.Error(t, err)
log.LogAttrs(context.Background(), slog.LevelInfo, "all attributes", errors.AttrsFromAll(err)...)
assert.Contains(t, w.String(), "msg=\"all attributes\"")
assert.Contains(t, w.String(), "foo=bar")
assert.Contains(t, w.String(), "code.filepath=")
assert.Contains(t, w.String(), "errors/attrs_test.go")
assert.Contains(t, w.String(), "code.function=github.com/kapetan-io/errors_test.TestAttrsAll")
assert.Contains(t, w.String(), "code.lineno=138")
assert.Contains(t, w.String(), "error=\"this is an error\"")
w.Reset()
log.LogAttrs(context.Background(), slog.LevelInfo, "all attributes",
errors.AttrsFromAll(errors.New("this is an error"))...)
assert.Contains(t, w.String(), "msg=\"all attributes\"")
assert.Contains(t, w.String(), "error=\"this is an error\"")
}
func TestAttrsWithErr(t *testing.T) {
var w bytes.Buffer
log := slog.New(slog.NewTextHandler(&w, &slog.HandlerOptions{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
}}))
err := errors.With("foo", "bar").Error("this is an error")
require.Error(t, err)
log.LogAttrs(context.Background(), slog.LevelInfo, "with error", errors.AttrsFromWithErr(err)...)
assert.Contains(t, w.String(), "msg=\"with error\"")
assert.Contains(t, w.String(), "foo=bar")
assert.Contains(t, w.String(), "error=\"this is an error\"")
w.Reset()
log.LogAttrs(context.Background(), slog.LevelInfo, "with error",
errors.AttrsFromAll(errors.New("this is an error"))...)
assert.Contains(t, w.String(), "msg=\"with error\"")
assert.Contains(t, w.String(), "error=\"this is an error\"")
}