From adec07ba7ec47abe3f30ac9a68af23badebcdf9d Mon Sep 17 00:00:00 2001 From: yangyile Date: Sat, 17 Aug 2024 16:18:17 +0700 Subject: [PATCH] =?UTF-8?q?=E5=86=8D=E6=AC=A1=E4=BF=AE=E6=94=B9=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- erkfc.go | 22 ++++++++++++++++++---- erkfc_test.go | 11 +++++++++-- erkrequire/erkrequire_test.go | 4 ++-- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/erkfc.go b/erkfc.go index d1ae51d..ccc0c5d 100644 --- a/erkfc.go +++ b/erkfc.go @@ -2,20 +2,34 @@ package erkkratos import "github.com/go-kratos/kratos/v2/errors" +// NewErkFs 指定错误的前缀让错误打印更加简单 func NewErkFs(erkFc func(format string, args ...interface{}) *errors.Error, msg string, opt string) func(erx error) *errors.Error { return func(erx error) *errors.Error { - return erkFc("%s%s%s", msg, opt, erx) + return erkFc("%s%s%s", msg, opt, erx).WithCause(erx) } } -func NewErkFb(erkFc func(format string, args ...interface{}) *errors.Error, msg string) func(erx error) *errors.Error { +func NewErkFsb(erkFc func(format string, args ...interface{}) *errors.Error, msg string) func(erx error) *errors.Error { return NewErkFs(erkFc, msg, " ") } -func NewErkFc(erkFc func(format string, args ...interface{}) *errors.Error, msg string) func(erx error) *errors.Error { +func NewErkFsc(erkFc func(format string, args ...interface{}) *errors.Error, msg string) func(erx error) *errors.Error { return NewErkFs(erkFc, msg, ":") } -func NewErkFe(erkFc func(format string, args ...interface{}) *errors.Error, msg string) func(erx error) *errors.Error { +func NewErkFse(erkFc func(format string, args ...interface{}) *errors.Error, msg string) func(erx error) *errors.Error { return NewErkFs(erkFc, msg, "=") } + +// NewErkFm 让错误返回的消息能够被前端直接展示,而把错误的细节放在 metadata 里面 +func NewErkFm(erkFc func(format string, args ...interface{}) *errors.Error, msg string, erk string) func(erx error) *errors.Error { + return func(erx error) *errors.Error { + return erkFc("%s", msg).WithCause(erx).WithMetadata(map[string]string{ + erk: erx.Error(), + }) + } +} + +func NewErkFmx(erkFc func(format string, args ...interface{}) *errors.Error, msg string) func(erx error) *errors.Error { + return NewErkFm(erkFc, msg, "erx") +} diff --git a/erkfc_test.go b/erkfc_test.go index 803aaec..8329a3e 100644 --- a/erkfc_test.go +++ b/erkfc_test.go @@ -8,8 +8,15 @@ import ( "github.com/yyle88/erero" ) -func TestNewErkFc(t *testing.T) { - erk := NewErkFc(errors_example.ErrorServerDbError, "erk")(erero.New("wac")) +func TestNewErkFsc(t *testing.T) { + erk := NewErkFsc(errors_example.ErrorServerDbError, "erk")(erero.New("wac")) + require.NotNil(t, erk) + require.True(t, errors_example.IsServerDbError(erk)) + t.Log(erk) +} + +func TestNewErkFmx(t *testing.T) { + erk := NewErkFmx(errors_example.ErrorServerDbError, "msg")(erero.New("wac")) require.NotNil(t, erk) require.True(t, errors_example.IsServerDbError(erk)) t.Log(erk) diff --git a/erkrequire/erkrequire_test.go b/erkrequire/erkrequire_test.go index 6f6fadf..7371af5 100644 --- a/erkrequire/erkrequire_test.go +++ b/erkrequire/erkrequire_test.go @@ -10,13 +10,13 @@ import ( "github.com/yyle88/erero" ) -func TestNoError(t *testing.T) { +func TestNoErrorFunction(t *testing.T) { var erk *errors.Error require.Error(t, erk) // 这是不符合预期的 erkrequire.NoError(t, erk) // 需要使用这个函数 } -func TestError(t *testing.T) { +func TestErrorFunction(t *testing.T) { var erk = errors_example.ErrorServerDbError("erx=%s", erero.New("wac")) erkrequire.Error(t, erk) }