-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
623 lines (517 loc) · 13.6 KB
/
errors_test.go
File metadata and controls
623 lines (517 loc) · 13.6 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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package errors
import (
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/yanun0323/errors/internal/colorize"
)
var errCause = New("root")
func TestNew(t *testing.T) {
err := New("test error").With("k1", "v1").With("k2", 2).(*errorStack)
if err == nil {
t.Fatal("Expected error, got nil")
}
expected := `
[error] test error
[cause] test error
[field]
[TestNew]
[k1] v1
[k2] 2
[stack]
[TestNew] /Users/Shared/Project/personal/go/errors/errors_test.go:16
`
f := FormatColorized(err)
f = colorize.ResetString(f)
if !strings.EqualFold(f, expected) {
t.Errorf("Expected colorized output '%s', but got '%s'", expected, f)
}
}
func TestErrorfBasic(t *testing.T) {
err := Errorf("formatted error %d", 123).With(
"k1", "v1",
).With(
"k2", 2,
"k3", 3,
).(*errorStack)
if err == nil {
t.Fatal("Expected error, got nil")
}
expected := `
[error] formatted error 123
[cause] formatted error 123
[field]
[TestErrorfBasic]
[k1] v1
[k2] 2
[k3] 3
[stack]
[TestErrorfBasic] /Users/Shared/Project/personal/go/errors/errors_test.go:40
`
f := FormatColorized(err)
f = colorize.ResetString(f)
if !strings.EqualFold(f, expected) {
t.Errorf("Expected colorized output '%s', but got '%s'", expected, f)
}
}
func causeError() error {
return Wrap(errCause).With("r1", "rv1").With("r2", 22)
}
func TestErrorfWrap(t *testing.T) {
err := Errorf("formatted error, err: %w", causeError()).
With("user_id", 123).
With(
"k1", "v1",
"k2", 2,
).(*errorStack)
if err == nil {
t.Fatal("Expected error, got nil")
}
expected := `
[error] formatted error, err: root
[cause] root
[field]
[causeError]
[r1] rv1
[r2] 22
[TestErrorfWrap]
[user_id] 123
[k1] v1
[k2] 2
[stack]
[causeError] /Users/Shared/Project/personal/go/errors/errors_test.go:70
[TestErrorfWrap] /Users/Shared/Project/personal/go/errors/errors_test.go:74
`
f := FormatColorized(err)
f = colorize.ResetString(f)
if !strings.EqualFold(f, expected) {
t.Errorf("Expected colorized output '%s', but got '%s'", expected, f)
}
}
func TestWrap(t *testing.T) {
wrappedErr := Wrap(causeError(), "wrapped").With(
"k1", "v1",
"k2", 2,
).With(
"k3", 3,
)
if unwrapped := Unwrap(wrappedErr); !Is(unwrapped, errCause) {
t.Error("Unwrap failed")
t.Errorf("unwrapped: %+v", Unwrap(unwrapped))
t.Errorf("baseErr: %+v", Unwrap(errCause))
}
expected := `
[error] wrapped, err: root
[cause] root
[field]
[causeError]
[r1] rv1
[r2] 22
[TestWrap]
[k1] v1
[k2] 2
[k3] 3
[stack]
[causeError] /Users/Shared/Project/personal/go/errors/errors_test.go:70
[TestWrap] /Users/Shared/Project/personal/go/errors/errors_test.go:108
`
f := FormatColorized(wrappedErr)
f = colorize.ResetString(f)
if !strings.EqualFold(f, expected) {
t.Errorf("Expected colorized output '%s', but got '%s'", expected, f)
}
}
func TestWrapf(t *testing.T) {
wrappedErr := Wrapf(causeError(), "wrapped %s", "world").With(
"k1", "v1",
"k2", 2,
).With(
"k3", 3,
)
if unwrapped := Unwrap(wrappedErr); !Is(unwrapped, errCause) {
t.Error("Unwrap failed")
t.Errorf("unwrapped: %+v", Unwrap(unwrapped))
t.Errorf("baseErr: %+v", Unwrap(errCause))
}
expected := `
[error] wrapped world, err: root
[cause] root
[field]
[causeError]
[r1] rv1
[r2] 22
[TestWrapf]
[k1] v1
[k2] 2
[k3] 3
[stack]
[causeError] /Users/Shared/Project/personal/go/errors/errors_test.go:70
[TestWrapf] /Users/Shared/Project/personal/go/errors/errors_test.go:145
`
f := FormatColorized(wrappedErr)
f = colorize.ResetString(f)
if !strings.EqualFold(f, expected) {
t.Errorf("Expected colorized output '%s', but got '%s'", expected, f)
}
}
func TestWith(t *testing.T) {
err := New("test error").With("user_id", 123).With("action", "create")
attrs := err.(*errorStack).attr
if len(attrs) != 2 {
t.Fatalf("Expected 2 attributes, got %d", len(attrs))
}
if attrs[0] != (attr{"TestWith", "user_id", 123}) {
t.Errorf("Expected user_id=123, got %v", attrs[0])
}
if attrs[1] != (attr{"TestWith", "action", "create"}) {
t.Errorf("Expected action=create, got %v", attrs[1])
}
}
func TestIs(t *testing.T) {
baseErr := New("base error")
wrappedErr := &errorStack{
message: "wrapped error",
cause: baseErr,
stack: getStack(0),
attr: []attr{},
}
if !Is(wrappedErr, baseErr) {
t.Error("Is should return true for wrapped error")
}
otherErr := New("other error")
if Is(wrappedErr, otherErr) {
t.Error("Is should return false for different error")
}
}
func TestAs(t *testing.T) {
customErr := &errorStack{
message: "custom error",
stack: getStack(0),
attr: []attr{},
}
wrappedErr := &errorStack{
message: "wrapped error",
cause: customErr,
stack: getStack(0),
attr: []attr{},
}
var target *errorStack
if !As(wrappedErr, &target) {
t.Error("As should return true for correct type")
}
if target != wrappedErr {
t.Errorf("As should set target to wrappedErr, got %v, expected %v", target, wrappedErr)
}
var otherTarget *OtherError
if As(wrappedErr, &otherTarget) {
t.Error("As should return false for non-matching type")
}
}
type OtherError struct {
msg string
}
func (e *OtherError) Error() string {
return e.msg
}
func TestString(t *testing.T) {
err := New("test error").With("key", "value")
str := err.(*errorStack).String()
if str != "test error" {
t.Errorf("String should contain error message, got: %s", str)
}
}
func TestJSON(t *testing.T) {
err := New("test error").With("key", "value")
jsonStr := FormatJson(err)
if !containsString(jsonStr, "test error") {
t.Error("JSON should contain error message")
}
if !containsString(jsonStr, "key") {
t.Error("JSON should contain fields")
}
}
func TestColorizedString(t *testing.T) {
err := New("test error").With("key", "value")
colorStr := FormatColorized(err)
if !containsString(colorStr, "test error") {
t.Error("ColorizedString should contain error message")
}
if !containsString(colorStr, "key") {
t.Error("ColorizedString should contain fields")
}
}
func TestStackTrace(t *testing.T) {
err := New("test error")
stack := err.(*errorStack).stack
if len(stack) == 0 {
t.Error("Expected stack trace")
}
if stack[0].Function == "" {
t.Error("Expected function name in stack trace")
}
}
// containsString checks if a string contains a substring
func containsString(s, substr string) bool {
return len(s) >= len(substr) && s != "" && findInString(s, substr)
}
func findInString(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
// Example demonstrates how to use the errors package
func Example() {
err1 := New("something went wrong")
fmt.Println(err1.Error())
err2 := &errorStack{
message: "failed to process user: something went wrong",
cause: err1,
stack: getStack(0),
attr: []attr{},
}
fmt.Println(err2.Error())
err3 := New("database error").
With("table", "users").
With("operation", "insert")
fmt.Println(err3.(*errorStack).String())
fmt.Println("JSON:", FormatJson(err3))
fmt.Println("Colorized:", FormatColorized(err3))
}
func TestBasicCreation(t *testing.T) {
err := New("database connection")
if err == nil {
t.Fatal("Expected error, got nil")
}
if err.Error() != "database connection" {
t.Errorf("Expected 'database connection', got '%s'", err.Error())
}
}
func TestErrorWrapping(t *testing.T) {
err1 := New("database connection")
err2 := Errorf("failed to create user: %w", err1)
if err2 == nil {
t.Fatal("Expected error, got nil")
}
if err2.Error() != "failed to create user: database connection" {
t.Errorf("Expected 'failed to create user: database connection', got '%s'", err2.Error())
}
}
func TestAdditionalFields(t *testing.T) {
err := New("user validation failed").
With("user_id", 12345).
With("email", "user@example.com").
With("attempt", 3).(*errorStack)
if err.Error() != "user validation failed" {
t.Errorf("Expected 'user validation failed', got '%s'", err.Error())
}
if len(err.attr) != 3 {
t.Fatalf("Expected 3 attributes, got %d", len(err.attr))
}
if err.attr[0] != (attr{"TestAdditionalFields", "user_id", 12345}) {
t.Errorf("Expected user_id=12345, got %v", err.attr[0])
}
if err.attr[1] != (attr{"TestAdditionalFields", "email", "user@example.com"}) {
t.Errorf("Expected email=user@example.com, got %v", err.attr[1])
}
if err.attr[2] != (attr{"TestAdditionalFields", "attempt", 3}) {
t.Errorf("Expected attempt=3, got %v", err.attr[2])
}
}
func TestChainChecking(t *testing.T) {
err := New("API call failed").
With("user_id", 12345).
With("email", "user@example.com").
With("attempt", 3).(*errorStack)
if err.Error() != "API call failed" {
t.Errorf("Expected 'API call failed', got '%s'", err.Error())
}
if len(err.attr) != 3 {
t.Fatalf("Expected 3 attributes, got %d", len(err.attr))
}
if err.attr[0] != (attr{"TestChainChecking", "user_id", 12345}) {
t.Errorf("Expected user_id=12345, got %v", err.attr[0])
}
if err.attr[1] != (attr{"TestChainChecking", "email", "user@example.com"}) {
t.Errorf("Expected email=user@example.com, got %v", err.attr[1])
}
if err.attr[2] != (attr{"TestChainChecking", "attempt", 3}) {
t.Errorf("Expected attempt=3, got %v", err.attr[2])
}
}
func TestFormat(t *testing.T) {
err := New("test error").With("key", "value")
formatted := Format(err)
expected := `
error:
test error
cause:
test error
field:
TestFormat:
key: value
stack:
TestFormat:
/Users/Shared/Project/personal/go/errors/errors_test.go:416 in TestFormat
`
if formatted != expected {
t.Errorf("Expected '%s', got '%s'", expected, formatted)
}
os.WriteFile("./test/format.txt", []byte(formatted), 0644)
}
func TestFormatJson(t *testing.T) {
err := New("user validation failed").
With("user_id", 12345).
With("email", "user@example.com").
With("attempt", 3)
expected := `{
"cause": "user validation failed",
"error": "user validation failed",
"field": [
{
"function": "TestFormatJson",
"key": "user_id",
"value": 12345
},
{
"function": "TestFormatJson",
"key": "email",
"value": "user@example.com"
},
{
"function": "TestFormatJson",
"key": "attempt",
"value": 3
}
],
"stack": [
{
"file": "/Users/Shared/Project/personal/go/errors/errors_test.go",
"function": "TestFormatJson",
"line": "440"
}
]
}`
if FormatJson(err) != expected {
t.Errorf("Expected JSON output, got '%s'", FormatJson(err))
}
os.WriteFile("./test/json.json", []byte(FormatJson(err)), 0644)
}
func TestFormatColorized(t *testing.T) {
err := New("user validation failed").
With("user_id", 12345).
With("email", "user@example.com").
With("attempt", 3)
expected := `
[error] user validation failed
[cause] user validation failed
[field]
[TestFormatColorized]
[user_id] 12345
[email] user@example.com
[attempt] 3
[stack]
[TestFormatColorized] /Users/Shared/Project/personal/go/errors/errors_test.go:482
`
f := FormatColorized(err)
f = colorize.ResetString(f)
if !strings.EqualFold(f, expected) {
t.Errorf("Expected colorized output, got '%s'", f)
}
os.WriteFile("./test/colorized.txt", []byte(f), 0644)
}
var errConnectionTimeout = New("database connection timeout")
func TestRealWorld(t *testing.T) {
err := processUser(123)
if err == nil {
t.Fatal("Expected error, got nil")
}
println(FormatColorized(err))
if !Is(err, errConnectionTimeout) {
t.Fatalf("Expected error to be a database connection timeout, got: %+v", err)
}
expected := "user validation, err: check time, user(123), err: database connection timeout"
if err.Error() != expected {
t.Fatalf("Expected '%s', got '%s'", expected, err.Error())
}
sErr := err.(*errorStack)
if len(sErr.attr) != 3 {
t.Fatalf("Expected 3 attributes, got %d", len(sErr.attr))
}
if sErr.attr[0] != (attr{"checkTime", "now", "2025-06-04 16:47:09"}) {
t.Errorf("Expected now=2025-06-04 16:47:09, got %v", sErr.attr[0])
}
if sErr.attr[1] != (attr{"validateUser", "user_id", 123}) {
t.Errorf("Expected user_id=123, got %v", sErr.attr[1])
}
if sErr.attr[2] != (attr{"validateUser", "table", "users"}) {
t.Errorf("Expected table=users, got %v", sErr.attr[2])
}
}
// processUser handles user-related operations
func processUser(userID int) error {
if err := validateUser(userID); err != nil {
return Errorf("user validation, err: %w", err)
}
return nil
}
// validateUser validates if user exists
func validateUser(userID int) error {
if userID == 0 {
return New("user not found").
With("user_id", userID).
With("table", "users")
}
if err := checkTime(); err != nil {
return Wrapf(err, "check time, user(%d)", userID).
With("user_id", userID).
With("table", "users")
}
return nil
}
func checkTime() error {
now := time.Now()
if now.Unix() > 0 {
err := Wrap(errConnectionTimeout).
With("now", "2025-06-04 16:47:09")
return err
}
return nil
}
func nilError() error {
return nil
}
func TestNil(t *testing.T) {
err := nilError()
if err != nil {
t.Fatal("nilError should be nil")
}
if Wrap(err, "hello") != nil {
t.Fatal("nilError should be nil")
}
if Wrapf(err, "hello %s", "world") != nil {
t.Fatal("nilError should be nil")
}
if Errorf("hello %w", err) != nil {
t.Fatal("nilError should be nil")
}
if Errorf("hello %s", "world") == nil {
t.Fatal("nilError should not be nil")
}
{
var err error
if Errorf("hello %w", err) != nil {
t.Fatal("nilError should be nil")
}
}
{
var err any
if Errorf("hello %w", err) != nil {
t.Fatal("nilError should be nil")
}
}
}