@@ -444,3 +444,133 @@ func TestWrap(t *testing.T) {
444
444
})
445
445
}
446
446
}
447
+
448
+ func TestCheckWithStack (t * testing.T ) {
449
+ tests := []struct {
450
+ name string
451
+ err error
452
+ wantErr string
453
+ }{
454
+ {
455
+ "nil error" ,
456
+ nil ,
457
+ "" ,
458
+ },
459
+ {
460
+ "regular error" ,
461
+ errors .New ("regular error" ),
462
+ "regular error" ,
463
+ },
464
+ {
465
+ "error with stack" ,
466
+ WithStack (errors .New ("with stack" )),
467
+ "with stack" ,
468
+ },
469
+ {
470
+ "error code" ,
471
+ ErrCodeUserNotFound ,
472
+ "[500201010: user not found]" ,
473
+ },
474
+ }
475
+
476
+ for _ , tt := range tests {
477
+ t .Run (tt .name , func (t * testing.T ) {
478
+ if tt .err == nil {
479
+ CheckWithStack (tt .err )
480
+ return
481
+ }
482
+
483
+ defer func () {
484
+ if r := recover (); r != nil {
485
+ err , ok := r .(error )
486
+ if ! ok {
487
+ t .Errorf ("Expected panic with error, got %v" , r )
488
+ return
489
+ }
490
+
491
+ if ! strings .Contains (err .Error (), tt .wantErr ) {
492
+ t .Errorf ("CheckWithStack() panicked with = %v, want containing %v" , err , tt .wantErr )
493
+ }
494
+
495
+ _ , hasStack := r .(* withStack )
496
+ if ! hasStack && ! stackExists (err ) {
497
+ t .Errorf ("CheckWithStack() error should have stack information" )
498
+ }
499
+ }
500
+ }()
501
+
502
+ CheckWithStack (tt .err )
503
+ t .Errorf ("CheckWithStack() did not panic as expected" )
504
+ })
505
+ }
506
+ }
507
+
508
+ func TestCheckWithWrap (t * testing.T ) {
509
+ tests := []struct {
510
+ name string
511
+ err error
512
+ format string
513
+ args []any
514
+ wantErr string
515
+ }{
516
+ {
517
+ "nil error" ,
518
+ nil ,
519
+ "format" ,
520
+ nil ,
521
+ "" ,
522
+ },
523
+ {
524
+ "regular error" ,
525
+ errors .New ("regular error" ),
526
+ "wrapped error: %s" ,
527
+ []any {"additional info" },
528
+ "wrapped error: additional info -> {regular error}" ,
529
+ },
530
+ {
531
+ "error with stack" ,
532
+ WithStack (errors .New ("with stack" )),
533
+ "wrapped error: %s" ,
534
+ []any {"additional info" },
535
+ "wrapped error: additional info -> {with stack}" ,
536
+ },
537
+ {
538
+ "error code" ,
539
+ ErrCodeUserNotFound ,
540
+ "wrapped error: %s" ,
541
+ []any {"additional info" },
542
+ "wrapped error: additional info -> {[500201010: user not found]}" ,
543
+ },
544
+ }
545
+
546
+ for _ , tt := range tests {
547
+ t .Run (tt .name , func (t * testing.T ) {
548
+ if tt .err == nil {
549
+ CheckWithWrap (tt .err , tt .format , tt .args ... )
550
+ return
551
+ }
552
+
553
+ defer func () {
554
+ if r := recover (); r != nil {
555
+ err , ok := r .(error )
556
+ if ! ok {
557
+ t .Errorf ("Expected panic with error, got %v" , r )
558
+ return
559
+ }
560
+
561
+ if ! strings .Contains (err .Error (), tt .wantErr ) {
562
+ t .Errorf ("CheckWithWrap() panicked with = %v, want containing %v" , err , tt .wantErr )
563
+ }
564
+
565
+ _ , hasStack := r .(* withStack )
566
+ if ! hasStack && ! stackExists (err ) {
567
+ t .Errorf ("CheckWithWrap() error should have stack information" )
568
+ }
569
+ }
570
+ }()
571
+
572
+ CheckWithWrap (tt .err , tt .format , tt .args ... )
573
+ t .Errorf ("CheckWithWrap() did not panic as expected" )
574
+ })
575
+ }
576
+ }
0 commit comments