-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoops_test.go
More file actions
194 lines (159 loc) · 4.32 KB
/
Copy pathoops_test.go
File metadata and controls
194 lines (159 loc) · 4.32 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
package oops
import (
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"testing"
)
func ExampleNew() {
l := logger()
err := New("oops").With("id", "new")
fmt.Println(err)
l.Error("error", "err", err)
// Output:
// oops
// level=ERROR msg=error err.err=oops err.id=new err.source="[{Function:github.com/jesse0michael/oops.ExampleNew File:oops_test.go Line:15}]"
}
func ExampleWrap() {
l := logger()
err := errors.New("oops")
err = Wrap(err, "id", "wrap")
fmt.Println(err)
l.Error("error", "err", err)
// Output:
// oops
// level=ERROR msg=error err.err=oops err.id=wrap err.source="[{Function:github.com/jesse0michael/oops.ExampleWrap File:oops_test.go Line:29}]"
}
func ExampleWrap_oopsError() {
l := logger()
oopsErr := New("oops").With("id", "new", "component", "example")
err := Wrap(oopsErr, "id", "wrap")
fmt.Println(err)
l.Error("error", "err", err)
// Output:
// oops
// level=ERROR msg=error err.err=oops err.component=example err.id=wrap err.source="[{Function:github.com/jesse0michael/oops.ExampleWrap_oopsError File:oops_test.go Line:42} {Function:github.com/jesse0michael/oops.ExampleWrap_oopsError File:oops_test.go Line:43}]"
}
func ExampleErrorf() {
l := logger()
err := Errorf("failure: %s, %w", "Errorf", errors.New("oops"))
fmt.Println(err)
l.Error("error", "err", err)
// Output:
// failure: Errorf, oops
// level=ERROR msg=error err.err="failure: Errorf, oops" err.source="[{Function:github.com/jesse0michael/oops.ExampleErrorf File:oops_test.go Line:56}]"
}
func ExampleErrorf_wrapOopsError() {
l := logger()
err := New("oops").With("id", "new", "component", "example")
err = Errorf("failure: %s, %w", "Errorf", err).With("id", "wrap")
fmt.Println(err)
l.Error("error", "err", err)
// Output:
// failure: Errorf, oops
// level=ERROR msg=error err.err="failure: Errorf, oops" err.component=example err.id=wrap err.source="[{Function:github.com/jesse0michael/oops.ExampleErrorf_wrapOopsError File:oops_test.go Line:70} {Function:github.com/jesse0michael/oops.ExampleErrorf_wrapOopsError File:oops_test.go Line:69}]"
}
func ExampleWrap_nil() {
l := logger()
err := Wrap(nil)
if err != nil {
fmt.Println(err)
}
l.Error("error", "err", err)
// Output:
// level=ERROR msg=error err=<nil>
}
func ExampleError_Code() {
l := logger()
err := New("oops").Code(404)
fmt.Println(err)
l.Error("error", "err", err)
fmt.Println(Code(err))
// Output:
// oops
// level=ERROR msg=error err.err=oops err.source="[{Function:github.com/jesse0michael/oops.ExampleError_Code File:oops_test.go Line:97}]"
// 404
}
func ExampleNew_nonOopsWrappedError() {
// This example shows how the LogValue won't be hit for a wrapped error :(
l := logger()
var err error
err = New("oops").With("id", "new", "component", "example")
err = &wrappedError{underlying: err}
fmt.Println(err)
l.Error("error", "err", err)
// Output:
// oops
// level=ERROR msg=error err=oops
}
// logger returns a slog logger to use in these example tests that removes non-deterministic and host-specific values.
func logger() *slog.Logger {
return slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
if a.Key == "time" {
return slog.Attr{}
}
if a.Key == "source" {
if s, ok := a.Value.Any().([]Source); ok {
for i, v := range s {
s[i].File = filepath.Base(v.File)
}
return slog.Any("source", s)
}
}
return a
},
}))
}
func TestCode(t *testing.T) {
tests := []struct {
name string
err error
want int
}{
{
name: "nil error",
err: nil,
want: 0,
},
{
name: "regular error",
err: errors.New("test-error"),
want: 0,
},
{
name: "oops error without code",
err: New("oops"),
want: 0,
},
{
name: "oops error with code",
err: New("oops").Code(404),
want: 404,
},
{
name: "Wrapped error",
err: errors.Join(errors.New("test-error"), Wrap(New("oops").Code(404))),
want: 404,
},
{
name: "oops error code attribute",
err: New("oops").With("code", 404),
want: 404,
},
{
name: "oops error non int code attribute",
err: New("oops").With("code", "NOT_FOUND"),
want: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Code(tt.err); got != tt.want {
t.Errorf("Code() = %v, want %v", got, tt.want)
}
})
}
}