-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
199 lines (174 loc) · 5.14 KB
/
errors_test.go
File metadata and controls
199 lines (174 loc) · 5.14 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
package errors
import (
stderrors "errors"
"io"
"testing"
grpcstatus "google.golang.org/grpc/status"
)
func TestWrap(t *testing.T) {
var 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 !stderrors.Is(wrapped1, base) {
t.Error("wrapped1 should match base via errors.Is")
}
if !stderrors.Is(wrapped2, wrapped1) {
t.Error("wrapped2 should match wrapped1 via errors.Is")
}
if !stderrors.Is(wrapped2, base) {
t.Error("wrapped2 should match base via errors.Is")
}
if !stderrors.Is(wrapped3, wrapped2) {
t.Error("wrapped3 should match wrapped2 via errors.Is")
}
if !stderrors.Is(wrapped3, wrapped1) {
t.Error("wrapped3 should match wrapped1 via errors.Is")
}
if !stderrors.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 !stderrors.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 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))
}
}