-
Notifications
You must be signed in to change notification settings - Fork 28
/
error_test.go
457 lines (413 loc) · 13.5 KB
/
error_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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package routine
import (
"errors"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRuntimeError_Goid(t *testing.T) {
goid := Goid()
err := NewRuntimeError(nil)
assert.Equal(t, goid, err.Goid())
task := GoWait(func(token CancelToken) {
assert.Equal(t, goid, err.Goid())
assert.NotEqual(t, Goid(), err.Goid())
})
task.Get()
}
func TestRuntimeError_Gopc(t *testing.T) {
gopc := *getg().gopc
err := NewRuntimeError(nil)
assert.Equal(t, gopc, err.Gopc())
task := GoWait(func(token CancelToken) {
assert.Equal(t, gopc, err.Gopc())
assert.NotEqual(t, *getg().gopc, err.Gopc())
})
task.Get()
}
func TestRuntimeError_Message(t *testing.T) {
err := NewRuntimeError(nil)
assert.Equal(t, "", err.Message())
err2 := NewRuntimeError("Hello")
assert.Equal(t, "Hello", err2.Message())
err3 := NewRuntimeError(&person{Id: 1, Name: "Tim"})
assert.Equal(t, "&{1 Tim}", err3.Message())
}
func TestRuntimeError_StackTrace(t *testing.T) {
err := NewRuntimeError(nil)
stackTrace := err.StackTrace()
capturedStackTrace := captureStackTrace(0, 200)
for i := 1; i < len(stackTrace); i++ {
assert.Equal(t, capturedStackTrace[i], stackTrace[i])
}
}
func TestRuntimeError_Panic_Panic(t *testing.T) {
defer func() {
cause := recover()
assert.NotNil(t, cause)
err := NewRuntimeError(cause)
lines := strings.Split(err.Error(), newLine)
assert.Equal(t, 6, len(lines))
//
line := lines[0]
assert.Equal(t, "RuntimeError: 1", line)
//
line = lines[1]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.TestRuntimeError_Panic_Panic."))
assert.True(t, strings.HasSuffix(line, "error_test.go:74"))
//
line = lines[2]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.TestRuntimeError_Panic_Panic()"))
assert.True(t, strings.HasSuffix(line, "error_test.go:77"))
}()
defer func() {
if cause := recover(); cause != nil {
panic(cause)
}
}()
panic(1)
}
func TestRuntimeError_Cause(t *testing.T) {
err := NewRuntimeError(nil)
assert.Nil(t, err.Cause())
err2 := NewRuntimeError(errors.New("error"))
assert.Nil(t, err2.Cause())
err3 := NewRuntimeError(&person{Id: 1, Name: "Tim"})
assert.Nil(t, err3.Cause())
err4 := NewRuntimeError(err)
assert.Same(t, err, err4.Cause())
}
func TestRuntimeError_Error_EmptyMessage_NilError(t *testing.T) {
err := NewRuntimeErrorWithMessageCause("", nil)
lines := strings.Split(err.Error(), newLine)
assert.Equal(t, 5, len(lines))
//
line := lines[0]
assert.Equal(t, "RuntimeError", line)
//
line = lines[1]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.TestRuntimeError_Error_EmptyMessage_NilError() in "))
assert.True(t, strings.HasSuffix(line, "error_test.go:95"))
//
line = lines[2]
assert.True(t, strings.HasPrefix(line, " at testing.tRunner() in "))
//
line = lines[3]
assert.Equal(t, " --- End of error stack trace ---", line)
//
line = lines[4]
assert.True(t, strings.HasPrefix(line, " created by testing.(*T).Run() in "))
}
func TestRuntimeError_Error_EmptyMessage_NormalError(t *testing.T) {
cause := NewRuntimeError("this is inner error")
err := NewRuntimeErrorWithMessageCause("", cause)
lines := strings.Split(err.Error(), newLine)
assert.Equal(t, 9, len(lines))
//
line := lines[0]
assert.Equal(t, "RuntimeError", line)
//
line = lines[1]
assert.Equal(t, " ---> RuntimeError: this is inner error", line)
//
line = lines[2]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.TestRuntimeError_Error_EmptyMessage_NormalError() in "))
assert.True(t, strings.HasSuffix(line, "error_test.go:117"))
//
line = lines[3]
assert.True(t, strings.HasPrefix(line, " at testing.tRunner() in "))
//
line = lines[4]
assert.Equal(t, " --- End of inner error stack trace ---", line)
//
line = lines[5]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.TestRuntimeError_Error_EmptyMessage_NormalError() in "))
assert.True(t, strings.HasSuffix(line, "error_test.go:118"))
//
line = lines[6]
assert.True(t, strings.HasPrefix(line, " at testing.tRunner() in "))
//
line = lines[7]
assert.Equal(t, " --- End of error stack trace ---", line)
//
line = lines[8]
assert.True(t, strings.HasPrefix(line, " created by testing.(*T).Run() in "))
}
func TestRuntimeError_Error_NormalMessage_NilError(t *testing.T) {
err := NewRuntimeErrorWithMessageCause("this is error message", nil)
lines := strings.Split(err.Error(), newLine)
assert.Equal(t, 5, len(lines))
//
line := lines[0]
assert.Equal(t, "RuntimeError: this is error message", line)
//
line = lines[1]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.TestRuntimeError_Error_NormalMessage_NilError() in "))
assert.True(t, strings.HasSuffix(line, "error_test.go:153"))
//
line = lines[2]
assert.True(t, strings.HasPrefix(line, " at testing.tRunner() in "))
//
line = lines[3]
assert.Equal(t, " --- End of error stack trace ---", line)
//
line = lines[4]
assert.True(t, strings.HasPrefix(line, " created by testing.(*T).Run() in "))
}
func TestRuntimeError_Error_NormalMessage_NormalError(t *testing.T) {
cause := NewRuntimeError("this is inner error")
err := NewRuntimeErrorWithMessageCause("this is error message", cause)
lines := strings.Split(err.Error(), newLine)
assert.Equal(t, 9, len(lines))
//
line := lines[0]
assert.Equal(t, "RuntimeError: this is error message", line)
//
line = lines[1]
assert.Equal(t, " ---> RuntimeError: this is inner error", line)
//
line = lines[2]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.TestRuntimeError_Error_NormalMessage_NormalError() in "))
assert.True(t, strings.HasSuffix(line, "error_test.go:175"))
//
line = lines[3]
assert.True(t, strings.HasPrefix(line, " at testing.tRunner() in "))
//
line = lines[4]
assert.Equal(t, " --- End of inner error stack trace ---", line)
//
line = lines[5]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.TestRuntimeError_Error_NormalMessage_NormalError() in "))
assert.True(t, strings.HasSuffix(line, "error_test.go:176"))
//
line = lines[6]
assert.True(t, strings.HasPrefix(line, " at testing.tRunner() in "))
//
line = lines[7]
assert.Equal(t, " --- End of error stack trace ---", line)
//
line = lines[8]
assert.True(t, strings.HasPrefix(line, " created by testing.(*T).Run() in "))
}
func TestRuntimeError_Error_NilStackTrace(t *testing.T) {
cause := NewRuntimeError("this is inner error")
cause.(*runtimeError).stackTrace = nil
err := NewRuntimeErrorWithMessageCause("this is error message", cause)
err.(*runtimeError).stackTrace = nil
lines := strings.Split(err.Error(), newLine)
assert.Equal(t, 5, len(lines))
//
line := lines[0]
assert.Equal(t, "RuntimeError: this is error message", line)
//
line = lines[1]
assert.Equal(t, " ---> RuntimeError: this is inner error", line)
//
line = lines[2]
assert.Equal(t, " --- End of inner error stack trace ---", line)
//
line = lines[3]
assert.Equal(t, " --- End of error stack trace ---", line)
//
line = lines[4]
assert.True(t, strings.HasPrefix(line, " created by testing.(*T).Run() in "))
}
func TestRuntimeError_Error_MainGoid(t *testing.T) {
err := NewRuntimeErrorWithMessageCause("this is error message", nil)
err.(*runtimeError).goid = 1
lines := strings.Split(err.Error(), newLine)
assert.Equal(t, 3, len(lines))
//
line := lines[0]
assert.Equal(t, "RuntimeError: this is error message", line)
//
line = lines[1]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.TestRuntimeError_Error_MainGoid() in "))
assert.True(t, strings.HasSuffix(line, "error_test.go:235"))
//
line = lines[2]
assert.True(t, strings.HasPrefix(line, " at testing.tRunner() in "))
}
func TestRuntimeError_Error_ZeroGopc(t *testing.T) {
err := NewRuntimeErrorWithMessageCause("this is error message", nil)
err.(*runtimeError).gopc = 0
lines := strings.Split(err.Error(), newLine)
assert.Equal(t, 3, len(lines))
//
line := lines[0]
assert.Equal(t, "RuntimeError: this is error message", line)
//
line = lines[1]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.TestRuntimeError_Error_ZeroGopc() in "))
assert.True(t, strings.HasSuffix(line, "error_test.go:252"))
//
line = lines[2]
assert.True(t, strings.HasPrefix(line, " at testing.tRunner() in "))
}
func TestArgumentNilError_Goid(t *testing.T) {
goid := Goid()
err := NewArgumentNilError("number", nil)
assert.Equal(t, goid, err.Goid())
task := GoWait(func(token CancelToken) {
assert.Equal(t, goid, err.Goid())
assert.NotEqual(t, Goid(), err.Goid())
})
task.Get()
}
func TestArgumentNilError_Gopc(t *testing.T) {
gopc := *getg().gopc
err := NewArgumentNilError("number", nil)
assert.Equal(t, gopc, err.Gopc())
task := GoWait(func(token CancelToken) {
assert.Equal(t, gopc, err.Gopc())
assert.NotEqual(t, *getg().gopc, err.Gopc())
})
task.Get()
}
func TestArgumentNilError_Message(t *testing.T) {
err := NewArgumentNilError("", nil)
assert.Equal(t, "Value cannot be null.", err.Message())
err2 := NewArgumentNilError("", "Hello")
assert.Equal(t, "Hello", err2.Message())
err3 := NewArgumentNilError("number", nil)
assert.Equal(t, "Value cannot be null."+newLine+"Parameter name: number.", err3.Message())
err4 := NewArgumentNilError("number", "Hello")
assert.Equal(t, "Hello"+newLine+"Parameter name: number.", err4.Message())
}
func TestArgumentNilError_StackTrace(t *testing.T) {
err := NewArgumentNilError("", nil)
stackTrace := err.StackTrace()
capturedStackTrace := captureStackTrace(0, 200)
for i := 1; i < len(stackTrace); i++ {
assert.Equal(t, capturedStackTrace[i], stackTrace[i])
}
}
func TestArgumentNilError_Panic_Panic(t *testing.T) {
defer func() {
cause := recover()
assert.NotNil(t, cause)
err := NewArgumentNilError("a", nil)
lines := strings.Split(err.Error(), newLine)
assert.Equal(t, 7, len(lines))
//
line := lines[0]
assert.Equal(t, "ArgumentNilError: Value cannot be null.", line)
//
line = lines[1]
assert.Equal(t, "Parameter name: a.", line)
//
line = lines[2]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.TestArgumentNilError_Panic_Panic."))
assert.True(t, strings.HasSuffix(line, "error_test.go:337"))
//
line = lines[3]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.TestArgumentNilError_Panic_Panic()"))
assert.True(t, strings.HasSuffix(line, "error_test.go:341"))
}()
defer func() {
if cause := recover(); cause != nil {
panic(cause)
}
}()
var a any
_ = a.(string)
}
func TestArgumentNilError_Cause(t *testing.T) {
err := NewArgumentNilError("", nil)
assert.Nil(t, err.Cause())
err2 := NewArgumentNilError("", errors.New("error"))
assert.Nil(t, err2.Cause())
err3 := NewArgumentNilError("", &person{Id: 1, Name: "Tim"})
assert.Nil(t, err3.Cause())
err4 := NewArgumentNilError("", err)
assert.Same(t, err, err4.Cause())
}
func TestArgumentNilError_Error(t *testing.T) {
cause := NewRuntimeError("this is inner error")
err := NewArgumentNilError("number", cause)
lines := strings.Split(err.Error(), newLine)
assert.Equal(t, 10, len(lines))
//
line := lines[0]
assert.Equal(t, "ArgumentNilError: Value cannot be null.", line)
//
line = lines[1]
assert.Equal(t, "Parameter name: number.", line)
//
line = lines[2]
assert.Equal(t, " ---> RuntimeError: this is inner error", line)
//
line = lines[3]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.TestArgumentNilError_Error() in "))
assert.True(t, strings.HasSuffix(line, "error_test.go:359"))
//
line = lines[4]
assert.True(t, strings.HasPrefix(line, " at testing.tRunner() in "))
//
line = lines[5]
assert.Equal(t, " --- End of inner error stack trace ---", line)
//
line = lines[6]
assert.True(t, strings.HasPrefix(line, " at github.com/timandy/routine.TestArgumentNilError_Error() in "))
assert.True(t, strings.HasSuffix(line, "error_test.go:360"))
//
line = lines[7]
assert.True(t, strings.HasPrefix(line, " at testing.tRunner() in "))
//
line = lines[8]
assert.Equal(t, " --- End of error stack trace ---", line)
//
line = lines[9]
assert.True(t, strings.HasPrefix(line, " created by testing.(*T).Run() in "))
}
func TestArgumentNilError_ParamName(t *testing.T) {
err := NewArgumentNilError("", nil)
assert.Equal(t, "", err.ParamName())
err2 := NewArgumentNilError("number", nil)
assert.Equal(t, "number", err2.ParamName())
}
type ArgumentNilError struct {
goid int64
gopc uintptr
message string
stackTrace []uintptr
cause RuntimeError
paramName string
}
func (ae *ArgumentNilError) Goid() int64 {
return ae.goid
}
func (ae *ArgumentNilError) Gopc() uintptr {
return ae.gopc
}
func (ae *ArgumentNilError) Message() string {
builder := &strings.Builder{}
if len(ae.message) == 0 {
builder.WriteString("Value cannot be null.")
} else {
builder.WriteString(ae.message)
}
if len(ae.paramName) != 0 {
builder.WriteString(newLine)
builder.WriteString("Parameter name: ")
builder.WriteString(ae.paramName)
builder.WriteString(".")
}
return builder.String()
}
func (ae *ArgumentNilError) StackTrace() []uintptr {
return ae.stackTrace
}
func (ae *ArgumentNilError) Cause() RuntimeError {
return ae.cause
}
func (ae *ArgumentNilError) Error() string {
return runtimeErrorError(ae)
}
func (ae *ArgumentNilError) ParamName() string {
return ae.paramName
}
func NewArgumentNilError(paramName string, cause any) *ArgumentNilError {
goid, gopc, msg, stackTrace, innerErr := runtimeErrorNew(cause)
return &ArgumentNilError{goid: goid, gopc: gopc, message: msg, paramName: paramName, stackTrace: stackTrace, cause: innerErr}
}