Skip to content

Commit ef6ab15

Browse files
committed
feat: 增加 CheckWithStack 和 CheckWithWrap
1 parent 648367e commit ef6ab15

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed

error.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,41 @@ func Check(err error) {
439439
panic(err)
440440
}
441441
}
442+
443+
func CheckWithStack(err error) {
444+
if err == nil {
445+
return
446+
}
447+
if stackExists(err) {
448+
panic(err)
449+
}
450+
if e, ok := err.(*withStack); ok {
451+
e.stack = callers()
452+
panic(e)
453+
}
454+
panic(&withStack{
455+
error: err,
456+
stack: callers(),
457+
})
458+
}
459+
460+
func CheckWithWrap(err error, format string, args ...any) {
461+
if err == nil {
462+
return
463+
}
464+
err = &withMessage{
465+
message: fmt.Sprintf(format, args...),
466+
cause: err,
467+
}
468+
if stackExists(err) {
469+
panic(err)
470+
}
471+
if e, ok := err.(*withStack); ok {
472+
e.stack = callers()
473+
panic(e)
474+
}
475+
panic(&withStack{
476+
error: err,
477+
stack: callers(),
478+
})
479+
}

error_test.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,133 @@ func TestWrap(t *testing.T) {
444444
})
445445
}
446446
}
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

Comments
 (0)