From 6a45bb2380f2a9b877bafedad65b437facd10861 Mon Sep 17 00:00:00 2001 From: cxbdasheng Date: Mon, 18 Aug 2025 10:25:04 +0800 Subject: [PATCH] =?UTF-8?q?test(liberr):=20=E6=B7=BB=E5=8A=A0=20ErrIsNil?= =?UTF-8?q?=20=E5=87=BD=E6=95=B0=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -验证 ErrIsNil 函数在不同错误和消息情况下的行为- 测试包括 nil 错误、非 nil 错误、自定义消息和多个消息等场景 - 验证使用 gf框架的 gerror.NewCode 创建的错误 - 测试不同类型的 gcode 错误和多种验证失败场景 --- library/liberr/err.go | 4 +- library/liberr/err_test.go | 164 +++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 library/liberr/err_test.go diff --git a/library/liberr/err.go b/library/liberr/err.go index dd5ec83..8f5214f 100644 --- a/library/liberr/err.go +++ b/library/liberr/err.go @@ -9,6 +9,7 @@ package liberr import ( "context" + "github.com/gogf/gf/v2/frame/g" ) @@ -18,7 +19,8 @@ func ErrIsNil(ctx context.Context, err error, msg ...string) { g.Log().Error(ctx, err.Error()) panic(msg[0]) } else { - panic(err.Error()) + //panic(err.Error()) + panic(err) } } } diff --git a/library/liberr/err_test.go b/library/liberr/err_test.go new file mode 100644 index 0000000..6741cb5 --- /dev/null +++ b/library/liberr/err_test.go @@ -0,0 +1,164 @@ +/* +* @desc:错误处理测试 +* @company:云南奇讯科技有限公司 +* @Author: yixiaohu +* @Date: 2022/3/2 14:53 + */ + +package liberr + +import ( + "context" + "errors" + "testing" + + "github.com/gogf/gf/v2/errors/gcode" + "github.com/gogf/gf/v2/errors/gerror" +) + +func TestErrIsNil(t *testing.T) { + ctx := context.Background() + + // Test case 1: 验证传入 nil 错误时不会 panic + t.Run("nil error", func(t *testing.T) { + defer func() { + if r := recover(); r != nil { + t.Errorf("ErrIsNil should not panic with nil error, but got: %v", r) + } + }() + ErrIsNil(ctx, nil) + }) + + // Test case 2: 验证传入非 nil 错误且无自定义消息时会 panic 并抛出原始错误 + t.Run("non-nil error without message", func(t *testing.T) { + testErr := errors.New("test error") + defer func() { + if r := recover(); r == nil { + t.Error("ErrIsNil should panic with non-nil error") + } else if r != testErr { + t.Errorf("Expected panic with error %v, but got: %v", testErr, r) + } + }() + ErrIsNil(ctx, testErr) + }) + + // Test case 3: 验证传入非 nil 错误和自定义消息时会 panic 并抛出自定义消息 + t.Run("non-nil error with custom message", func(t *testing.T) { + testErr := errors.New("test error") + customMsg := "custom error message" + defer func() { + if r := recover(); r == nil { + t.Error("ErrIsNil should panic with non-nil error and custom message") + } else if r != customMsg { + t.Errorf("Expected panic with message %q, but got: %v", customMsg, r) + } + }() + ErrIsNil(ctx, testErr, customMsg) + }) + + // Test case 4: 验证传入多个消息时使用第一个消息 + t.Run("non-nil error with multiple messages", func(t *testing.T) { + testErr := errors.New("test error") + firstMsg := "first message" + secondMsg := "second message" + defer func() { + if r := recover(); r == nil { + t.Error("ErrIsNil should panic with non-nil error and messages") + } else if r != firstMsg { + t.Errorf("Expected panic with first message %q, but got: %v", firstMsg, r) + } + }() + ErrIsNil(ctx, testErr, firstMsg, secondMsg) + }) + + // Test case 5: 验证 gf 框架的 gerror.NewCode 错误处理 - 验证码场景 + t.Run("gerror with CodeValidationFailed", func(t *testing.T) { + testErr := gerror.NewCode(gcode.CodeValidationFailed, "验证码已过期或不存在") + defer func() { + if r := recover(); r == nil { + t.Error("ErrIsNil should panic with gerror") + } else if r != testErr { + t.Errorf("Expected panic with gerror %v, but got: %v", testErr, r) + } + }() + ErrIsNil(ctx, testErr) + }) + + // Test case 6: 验证 gerror.NewCode 错误与自定义消息 + t.Run("gerror with custom message", func(t *testing.T) { + testErr := gerror.NewCode(gcode.CodeValidationFailed, "验证码已过期或不存在") + customMsg := "验证失败" + defer func() { + if r := recover(); r == nil { + t.Error("ErrIsNil should panic with gerror and custom message") + } else if r != customMsg { + t.Errorf("Expected panic with message %q, but got: %v", customMsg, r) + } + }() + ErrIsNil(ctx, testErr, customMsg) + }) + + // Test case 7: 验证不同类型的 gcode 错误 + t.Run("gerror with CodeInternalError", func(t *testing.T) { + testErr := gerror.NewCode(gcode.CodeInternalError, "内部服务错误") + defer func() { + if r := recover(); r == nil { + t.Error("ErrIsNil should panic with CodeInternalError") + } else if r != testErr { + t.Errorf("Expected panic with gerror %v, but got: %v", testErr, r) + } + }() + ErrIsNil(ctx, testErr) + }) + + // Test case 8: 验证多种验证失败场景 + t.Run("multiple validation scenarios", func(t *testing.T) { + scenarios := []struct { + name string + err error + msg string + }{ + { + name: "验证码过期", + err: gerror.NewCode(gcode.CodeValidationFailed, "验证码已过期或不存在"), + msg: "验证码验证失败", + }, + { + name: "参数验证失败", + err: gerror.NewCode(gcode.CodeValidationFailed, "参数格式不正确"), + msg: "请求参数错误", + }, + { + name: "业务验证失败", + err: gerror.NewCode(gcode.CodeValidationFailed, "用户状态异常"), + msg: "", + }, + } + + for _, scenario := range scenarios { + t.Run(scenario.name, func(t *testing.T) { + defer func() { + if r := recover(); r == nil { + t.Error("ErrIsNil should panic") + } else { + if scenario.msg != "" { + if r != scenario.msg { + t.Errorf("Expected panic with message %q, but got: %v", scenario.msg, r) + } + } else { + if r != scenario.err { + t.Errorf("Expected panic with error %v, but got: %v", scenario.err, r) + } + } + } + }() + + if scenario.msg != "" { + ErrIsNil(ctx, scenario.err, scenario.msg) + } else { + ErrIsNil(ctx, scenario.err) + } + }) + } + }) +}