-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
304 lines (265 loc) · 7.7 KB
/
errors_test.go
File metadata and controls
304 lines (265 loc) · 7.7 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
package errors
import (
stderrors "errors"
"io"
"testing"
grpcstatus "google.golang.org/grpc/status"
)
func TestWrap(t *testing.T) {
tests := []struct {
name string
err error
message string
expected string
}{
{
"original error is wrapped",
io.EOF,
"read error",
"read error: EOF",
},
{
"wrapping a wrapped error results in an error wrapped twice",
Wrap(io.EOF, "read error"),
"client error",
"client error: read error: EOF",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := Wrap(tt.err, tt.message)
if err.Error() != tt.expected {
t.Errorf("(%+v, %+v): expected %+v, got %+v", tt.err, tt.message, tt.expected, err)
}
// ensure GRPC status msg has wrapped content no matter you wrap how many times
if grpcstatus.Convert(err).Message() != tt.expected {
t.Errorf("GRPC status msg expected %+v, got %+v", tt.expected, grpcstatus.Convert(err).Message())
}
})
}
}
func TestErrorsIs(t *testing.T) {
// errors.Is should work through the full wrap chain
base := stderrors.New("base error")
wrapped1 := Wrap(base, "layer1")
wrapped2 := Wrap(wrapped1, "layer2")
wrapped3 := Wrap(wrapped2, "layer3")
if !Is(wrapped1, base) {
t.Error("wrapped1 should match base via errors.Is")
}
if !Is(wrapped2, wrapped1) {
t.Error("wrapped2 should match wrapped1 via errors.Is")
}
if !Is(wrapped2, base) {
t.Error("wrapped2 should match base via errors.Is")
}
if !Is(wrapped3, wrapped2) {
t.Error("wrapped3 should match wrapped2 via errors.Is")
}
if !Is(wrapped3, wrapped1) {
t.Error("wrapped3 should match wrapped1 via errors.Is")
}
if !Is(wrapped3, base) {
t.Error("wrapped3 should match base via errors.Is")
}
}
func TestCauseStillReturnsRoot(t *testing.T) {
base := stderrors.New("root")
wrapped1 := Wrap(base, "a")
wrapped2 := Wrap(wrapped1, "b")
// Cause() should still return the root error for backward compatibility
if wrapped1.Cause() != base {
t.Errorf("wrapped1.Cause() = %v, want %v", wrapped1.Cause(), base)
}
if wrapped2.Cause() != base {
t.Errorf("wrapped2.Cause() = %v, want %v", wrapped2.Cause(), base)
}
}
func TestNewf(t *testing.T) {
err := Newf("error %d: %s", 42, "test")
if err.Error() != "error 42: test" {
t.Errorf("Newf() = %q, want %q", err.Error(), "error 42: test")
}
}
func TestWrapf(t *testing.T) {
base := stderrors.New("base")
err := Wrapf(base, "context %d", 1)
expected := "context 1: base"
if err.Error() != expected {
t.Errorf("Wrapf() = %q, want %q", err.Error(), expected)
}
if !Is(err, base) {
t.Error("Wrapf result should match base via errors.Is")
}
}
func TestStackDepthCapped(t *testing.T) {
err := New("test")
// Assert on Callers (PCs) since StackFrame may expand due to inlining
if len(err.Callers()) > defaultStackDepth {
t.Errorf("callers depth %d exceeds max %d", len(err.Callers()), defaultStackDepth)
}
// StackFrame is also capped at len(pcs)
if len(err.StackFrame()) > defaultStackDepth {
t.Errorf("frame depth %d exceeds max %d", len(err.StackFrame()), defaultStackDepth)
}
}
func TestStackDepthCappedDeep(t *testing.T) {
var createDeepError func(depth int) ErrorExt
createDeepError = func(depth int) ErrorExt {
if depth == 0 {
return New("deep error")
}
return createDeepError(depth - 1)
}
err := createDeepError(100)
if len(err.Callers()) > defaultStackDepth {
t.Errorf("callers depth %d exceeds max %d", len(err.Callers()), defaultStackDepth)
}
if len(err.Callers()) == 0 {
t.Error("callers should not be empty")
}
}
func TestSetMaxStackDepth(t *testing.T) {
// Save and restore
prev := atomicStackDepth.Load()
defer atomicStackDepth.Store(prev)
SetMaxStackDepth(8)
if got := atomicStackDepth.Load(); got != 8 {
t.Fatalf("expected depth 8, got %d", got)
}
// Zero is ignored
SetMaxStackDepth(0)
if got := atomicStackDepth.Load(); got != 8 {
t.Fatalf("expected depth 8 (unchanged), got %d", got)
}
// Negative is ignored
SetMaxStackDepth(-1)
if got := atomicStackDepth.Load(); got != 8 {
t.Fatalf("expected depth 8 (unchanged), got %d", got)
}
// Over 256 is ignored
SetMaxStackDepth(1000)
if got := atomicStackDepth.Load(); got != 8 {
t.Fatalf("expected depth 8 (unchanged), got %d", got)
}
// 256 is accepted
SetMaxStackDepth(256)
if got := atomicStackDepth.Load(); got != 256 {
t.Fatalf("expected depth 256, got %d", got)
}
}
func TestIs(t *testing.T) {
base := stderrors.New("base")
wrapped := Wrap(base, "wrapped")
if !Is(wrapped, base) {
t.Error("Is should find base through ColdBrew wrap")
}
if Is(wrapped, stderrors.New("other")) {
t.Error("Is should not match unrelated error")
}
if !Is(wrapped, wrapped) {
t.Error("Is should match error against itself")
}
}
func TestAs(t *testing.T) {
grpcErr := NewWithStatus("not found", grpcstatus.New(5, "not found"))
wrapped := Wrap(grpcErr, "lookup failed")
var ext ErrorExt
if !As(wrapped, &ext) {
t.Fatal("As should find ErrorExt in chain")
}
if ext.Error() != "lookup failed: not found" {
t.Errorf("As target = %q, want %q", ext.Error(), "lookup failed: not found")
}
}
func TestUnwrap(t *testing.T) {
base := stderrors.New("base")
wrapped := Wrap(base, "ctx")
unwrapped := Unwrap(wrapped)
if unwrapped == nil {
t.Fatal("Unwrap should return non-nil for wrapped error")
}
if unwrapped != base {
t.Errorf("Unwrap = %v, want %v", unwrapped, base)
}
// Unwrap on a non-wrapping error returns nil
plain := stderrors.New("plain")
if Unwrap(plain) != nil {
t.Error("Unwrap on non-wrapping error should return nil")
}
}
func TestJoin(t *testing.T) {
err1 := New("first")
err2 := stderrors.New("second")
joined := Join(err1, err2)
if joined == nil {
t.Fatal("Join should return non-nil")
}
if !Is(joined, err1) {
t.Error("Join result should contain first error")
}
if !Is(joined, err2) {
t.Error("Join result should contain second error")
}
// All nils returns nil
if Join(nil, nil) != nil {
t.Error("Join of all nils should return nil")
}
}
func TestErrUnsupported(t *testing.T) {
err := stderrors.ErrUnsupported
if !Is(err, ErrUnsupported) {
t.Error("ErrUnsupported should match stdlib ErrUnsupported via Is")
}
}
func TestCauseStandalone(t *testing.T) {
// Works on ColdBrew error chain
root := stderrors.New("root")
a := Wrap(root, "a")
b := Wrap(a, "b")
if got := Cause(b); got != root {
t.Errorf("Cause(b) = %v, want %v", got, root)
}
if got := Cause(a); got != root {
t.Errorf("Cause(a) = %v, want %v", got, root)
}
// Works on plain stdlib errors (no Unwrap)
plain := stderrors.New("plain")
if got := Cause(plain); got != plain {
t.Errorf("Cause(plain) = %v, want %v", got, plain)
}
// Returns nil for nil
if got := Cause(nil); got != nil {
t.Errorf("Cause(nil) = %v, want nil", got)
}
// Join multi-errors use Unwrap() []error, not Unwrap() error,
// so single-error Unwrap returns nil and Cause returns the Join itself.
joined := stderrors.Join(io.EOF)
if got := Cause(joined); got != joined {
t.Errorf("Cause(joinedErr) = %v, want %v", got, joined)
}
}
func TestStackFrameConsistency(t *testing.T) {
err := New("consistency test")
// First call resolves lazily
frames1 := err.StackFrame()
// Second call returns cached result
frames2 := err.StackFrame()
if len(frames1) != len(frames2) {
t.Fatalf("frame count changed: %d vs %d", len(frames1), len(frames2))
}
for i := range frames1 {
if frames1[i] != frames2[i] {
t.Fatalf("frame %d differs: %+v vs %+v", i, frames1[i], frames2[i])
}
}
// Callers should be non-empty
pcs := err.Callers()
if len(pcs) == 0 {
t.Fatal("Callers() should not be empty")
}
// Frames should not exceed PCs (inlining can expand, but resolveFrames caps)
if len(frames1) > len(pcs) {
t.Fatalf("StackFrame count %d exceeds Callers count %d", len(frames1), len(pcs))
}
}