forked from abyssparanoia/goerr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
223 lines (199 loc) · 5.98 KB
/
errors_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
package goerr_test
import (
"bytes"
"errors"
"fmt"
"log/slog"
"regexp"
"strings"
"testing"
"github.com/reproio/goerr"
)
func oops() *goerr.Error {
return goerr.New("omg")
}
func normalError() error {
return fmt.Errorf("red")
}
func wrapError() *goerr.Error {
err := normalError()
return goerr.Wrap(err, "orange")
}
func TestNew(t *testing.T) {
err := oops()
v := fmt.Sprintf("%+v", err)
if !strings.Contains(v, "goerr_test.oops") {
t.Error("Stack trace 'goerr_test.oops' is not found")
}
if !strings.Contains(err.Error(), "omg") {
t.Error("Error message is not correct")
}
}
func TestWrapError(t *testing.T) {
err := wrapError()
st := fmt.Sprintf("%+v", err)
if !strings.Contains(st, "github.com/reproio/goerr_test.wrapError") {
t.Error("Stack trace 'wrapError' is not found")
}
if !strings.Contains(st, "github.com/reproio/goerr_test.TestWrapError") {
t.Error("Stack trace 'TestWrapError' is not found")
}
if strings.Contains(st, "github.com/reproio/goerr_test.normalError") {
t.Error("Stack trace 'normalError' is found")
}
if !strings.Contains(err.Error(), "orange: red") {
t.Error("Error message is not correct")
}
}
func TestStackTrace(t *testing.T) {
err := oops()
st := err.Stacks()
if len(st) != 4 {
t.Errorf("Expected stack length of 4, got %d", len(st))
}
if st[0].Func != "github.com/reproio/goerr_test.oops" {
t.Error("Stack trace 'github.com/reproio/goerr_test.oops' is not found")
}
if !regexp.MustCompile(`/goerr/errors_test\.go$`).MatchString(st[0].File) {
t.Error("Stack trace file is not correct")
}
if st[0].Line != 16 {
t.Errorf("Expected line number 13, got %d", st[0].Line)
}
}
func TestMultiWrap(t *testing.T) {
err1 := oops()
err2 := goerr.Wrap(err1)
if err1 == err2 {
t.Error("Expected err1 and err2 to be different")
}
err3 := goerr.Wrap(err1, "some message")
if err1 == err3 {
t.Error("Expected err1 and err3 to be different")
}
}
func TestErrorCode(t *testing.T) {
rootErr := goerr.New("something bad")
baseErr1 := goerr.New("oops").ID("code1")
baseErr2 := goerr.New("oops").ID("code2")
newErr := baseErr1.Wrap(rootErr).WithValue("v", 1)
if !errors.Is(newErr, baseErr1) {
t.Error("Expected newErr to be based on baseErr1")
}
if newErr == baseErr1 {
t.Error("Expected newErr and baseErr1 to be different")
}
if newErr.Values()["v"] == nil {
t.Error("Expected newErr to have a non-nil value for 'v'")
}
if baseErr1.Values()["v"] != nil {
t.Error("Expected baseErr1 to have a nil value for 'v'")
}
if errors.Is(newErr, baseErr2) {
t.Error("Expected newErr to not be based on baseErr2")
}
}
func TestPrintable(t *testing.T) {
cause := errors.New("test")
err := goerr.Wrap(cause, "oops").ID("E001").WithValue("blue", "five")
p := err.Printable()
if p.Message != "oops" {
t.Errorf("Expected message to be 'oops', got '%s'", p.Message)
}
if p.ID != "E001" {
t.Errorf("Expected ID to be 'E001', got '%s'", p.ID)
}
if p.Cause != cause {
t.Errorf("Expected cause to be '%v', got '%v'", cause, p.Cause)
}
if p.Values["blue"] != "five" {
t.Errorf("Expected value for 'blue' to be 'five', got '%v'", p.Values["blue"])
}
}
func TestUnwrap(t *testing.T) {
err1 := goerr.New("omg").WithValue("color", "five")
err2 := fmt.Errorf("oops: %w", err1)
err := goerr.Unwrap(err2)
if err == nil {
t.Error("Expected unwrapped error to be non-nil")
}
values := err.Values()
if values["color"] != "five" {
t.Errorf("Expected value for 'color' to be 'five', got '%v'", values["color"])
}
}
func TestFormat(t *testing.T) {
err := goerr.New("test: %s", "blue")
if err.Error() != "test: blue" {
t.Errorf("Expected error message to be 'test: blue', got '%s'", err.Error())
}
}
func TestErrorString(t *testing.T) {
err := goerr.Wrap(goerr.Wrap(goerr.New("blue"), "orange"), "red")
if err.Error() != "red: orange: blue" {
t.Errorf("Expected error message to be 'red: orange: blue', got '%s'", err.Error())
}
}
func TestLoggingNestedError(t *testing.T) {
err1 := goerr.New("e1").WithValue("color", "orange")
err2 := goerr.Wrap(err1, "e2").WithValue("number", "five")
out := &bytes.Buffer{}
logger := slog.New(slog.NewJSONHandler(out, nil))
logger.Error("fail", slog.Any("error", err2))
if !strings.Contains(out.String(), `"number":"five"`) {
t.Errorf("Expected log output to contain '\"number\":\"five\"', got '%s'", out.String())
}
if !strings.Contains(out.String(), `"color":"orange"`) {
t.Errorf("Expected log output to contain '\"color\":\"orange\"', got '%s'", out.String())
}
}
func TestLoggerWithNil(t *testing.T) {
out := &bytes.Buffer{}
var err *goerr.Error
logger := slog.New(slog.NewJSONHandler(out, nil))
logger.Error("fail", slog.Any("error", err))
if !strings.Contains(out.String(), `"error":null`) {
t.Errorf("Expected log output to contain '\"error\":null', got '%s'", out.String())
}
}
func TestUnstack(t *testing.T) {
t.Run("original stack", func(t *testing.T) {
err := oops()
st := err.Stacks()
if st == nil {
t.Error("Expected stack trace to be nil")
}
if len(st) == 0 {
t.Error("Expected stack trace length to be 0")
}
if st[0].Func != "github.com/reproio/goerr_test.oops" {
t.Errorf("Not expected stack trace func name (github.com/reproio/goerr_test.oops): %s", st[0].Func)
}
})
t.Run("unstacked", func(t *testing.T) {
err := oops().Unstack()
st1 := err.Stacks()
if st1 == nil {
t.Error("Expected stack trace to be non-nil")
}
if len(st1) == 0 {
t.Error("Expected stack trace length to be non-zero")
}
if st1[0].Func != "github.com/reproio/goerr_test.TestUnstack.func2" {
t.Errorf("Not expected stack trace func name (github.com/reproio/goerr_test.TestUnstack.func2): %s", st1[0].Func)
}
})
t.Run("unstackN with 2", func(t *testing.T) {
err := oops().UnstackN(2)
st2 := err.Stacks()
if st2 == nil {
t.Error("Expected stack trace to be non-nil")
}
if len(st2) == 0 {
t.Error("Expected stack trace length to be non-zero")
}
if st2[0].Func != "testing.tRunner" {
t.Errorf("Not expected stack trace func name (testing.tRunner): %s", st2[0].Func)
}
})
}