From 72f9b1dc43835a22439b7409b87257ee5c68bb4f Mon Sep 17 00:00:00 2001 From: yangyile Date: Mon, 30 Sep 2024 18:24:13 +0700 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- erk_as.go | 19 ++++++++++++ erk_as_test.go | 39 ++++++++++++++++++++++++ erkrequire/erk_require.go | 16 ++++++---- go.mod | 4 +-- go.sum | 12 ++++---- internal/utils/utils.go | 3 ++ new_emk.go | 44 +++++++++++++++++++++++++++ new_emk_test.go | 17 +++++++++++ new_erk.go | 63 ++++++++++++++++++++------------------- new_erk_test.go | 47 ++--------------------------- 10 files changed, 176 insertions(+), 88 deletions(-) create mode 100644 erk_as.go create mode 100644 erk_as_test.go create mode 100644 internal/utils/utils.go create mode 100644 new_emk.go create mode 100644 new_emk_test.go diff --git a/erk_as.go b/erk_as.go new file mode 100644 index 0000000..3352ba6 --- /dev/null +++ b/erk_as.go @@ -0,0 +1,19 @@ +package erkkratos + +import ( + "github.com/go-kratos/kratos/v2/errors" + "github.com/orzkratos/erkkratos/internal/utils" +) + +// As 这里使用As就直接能指定类型,这样能够简便些,毕竟在这个语境下的目标类型确定 +func As(erx error) (erk *errors.Error, ok bool) { + return erk, errors.As(erx, &erk) +} + +// Is 这里比较相等,直接使用确定的类型,假如要比较不确定的类型,就请用原始的就行 +func Is(erx *errors.Error, target *errors.Error) bool { + if erx == nil || target == nil { + return utils.NewBoolean(erx == nil && target == nil) + } + return erx.Is(target) +} diff --git a/erk_as_test.go b/erk_as_test.go new file mode 100644 index 0000000..dba7ab5 --- /dev/null +++ b/erk_as_test.go @@ -0,0 +1,39 @@ +package erkkratos + +import ( + "testing" + + "github.com/go-kratos/kratos/v2/errors" + "github.com/orzkratos/erkkratos/internal/errors_example" + "github.com/stretchr/testify/require" + "github.com/yyle88/erero" +) + +func TestAs(t *testing.T) { + { + var erk = errors_example.ErrorServerDbError("wrong") + var erx error = erk + res, ok := As(erx) + require.True(t, ok) + t.Log(res) + require.NotNil(t, res) + } + + { + var erk *errors.Error + var erx error = erk + res, ok := As(erx) + require.True(t, ok) + t.Log(res) + require.Nil(t, res) + } +} + +func TestIs(t *testing.T) { + erk1 := errors_example.ErrorServerDbError("wrong-1") + erk2 := errors_example.ErrorServerDbError("wrong-2") + require.True(t, Is(erk1, erk2)) + + require.True(t, errors.Is(erk1, erk1)) //还是相等 + require.True(t, erero.Ise(erk1, erk1)) //依然相等 +} diff --git a/erkrequire/erk_require.go b/erkrequire/erk_require.go index 435dfff..bb342f1 100644 --- a/erkrequire/erk_require.go +++ b/erkrequire/erk_require.go @@ -2,6 +2,7 @@ package erkrequire import ( "github.com/go-kratos/kratos/v2/errors" + "github.com/orzkratos/erkkratos/internal/utils" "github.com/stretchr/testify/require" ) @@ -20,10 +21,15 @@ func eke(erk *errors.Error) error { return erk } -func Is(t require.TestingT, expected *errors.Error, actualEx *errors.Error, msgAndArgs ...interface{}) { - require.Equal(t, bool(eke(expected) == nil), bool(eke(actualEx) == nil), msgAndArgs...) - if expected != nil && actualEx != nil { - require.Equal(t, expected.Reason, actualEx.Reason, msgAndArgs...) - require.Equal(t, expected.Code, actualEx.Code, msgAndArgs...) +func Is(t require.TestingT, expected *errors.Error, inputErk *errors.Error, msgAndArgs ...interface{}) { + require.Equal( + t, + utils.NewBoolean(eke(expected) == nil), + utils.NewBoolean(eke(inputErk) == nil), + msgAndArgs..., + ) + if expected != nil && inputErk != nil { + require.Equal(t, expected.Reason, inputErk.Reason, msgAndArgs...) + require.Equal(t, expected.Code, inputErk.Code, msgAndArgs...) } } diff --git a/go.mod b/go.mod index 032a0a3..ab1301b 100644 --- a/go.mod +++ b/go.mod @@ -18,8 +18,8 @@ require ( go.uber.org/zap v1.27.0 // indirect golang.org/x/sys v0.25.0 // indirect golang.org/x/text v0.18.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect - google.golang.org/grpc v1.66.2 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240924160255-9d4c2d233b61 // indirect + google.golang.org/grpc v1.67.0 // indirect google.golang.org/protobuf v1.34.2 // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 9b23ef5..7c41e82 100644 --- a/go.sum +++ b/go.sum @@ -28,16 +28,16 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= -golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= -golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= +golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= +golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 h1:pPJltXNxVzT4pK9yD8vR9X75DaWYYmLGMsEvBfFQZzQ= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= -google.golang.org/grpc v1.66.2 h1:3QdXkuq3Bkh7w+ywLdLvM56cmGvQHUMZpiCzt6Rqaoo= -google.golang.org/grpc v1.66.2/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240924160255-9d4c2d233b61 h1:N9BgCIAUvn/M+p4NJccWPWb3BWh88+zyL0ll9HgbEeM= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240924160255-9d4c2d233b61/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= +google.golang.org/grpc v1.67.0 h1:IdH9y6PF5MPSdAntIcpjQ+tXO41pcQsfZV2RxtQgVcw= +google.golang.org/grpc v1.67.0/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/utils/utils.go b/internal/utils/utils.go new file mode 100644 index 0000000..16a9cd3 --- /dev/null +++ b/internal/utils/utils.go @@ -0,0 +1,3 @@ +package utils + +func NewBoolean(b bool) bool { return b } diff --git a/new_emk.go b/new_emk.go new file mode 100644 index 0000000..82167f9 --- /dev/null +++ b/new_emk.go @@ -0,0 +1,44 @@ +package erkkratos + +import "github.com/go-kratos/kratos/v2/errors" + +// EmtBottle 封装错误处理所需的属性 +type EmtBottle struct { + efc func(format string, args ...interface{}) *errors.Error + message string + metaKeyName string +} + +// NewEmtBottle 创建一个新的 EmtBottle 实例 +func NewEmtBottle(efc func(format string, args ...interface{}) *errors.Error, message string, metaKeyName string) *EmtBottle { + return &EmtBottle{ + efc: efc, + message: message, + metaKeyName: metaKeyName, + } +} + +// SetErkFunc 设置 efc 属性并返回自身,以支持链式调用 +func (e *EmtBottle) SetErkFunc(efc func(format string, args ...interface{}) *errors.Error) *EmtBottle { + e.efc = efc + return e +} + +// SetMessage 设置 message 属性并返回自身,以支持链式调用 +func (e *EmtBottle) SetMessage(message string) *EmtBottle { + e.message = message + return e +} + +// SetMetaKeyName 设置 metaKeyName 属性并返回自身,以支持链式调用 +func (e *EmtBottle) SetMetaKeyName(metaKeyName string) *EmtBottle { + e.metaKeyName = metaKeyName + return e +} + +// Wrap 方法用于包装错误并返回格式化的错误信息和元数据 +func (e *EmtBottle) Wrap(erx error) *errors.Error { + return e.efc("%s", e.message).WithCause(erx).WithMetadata(map[string]string{ + e.metaKeyName: erx.Error(), + }) +} diff --git a/new_emk_test.go b/new_emk_test.go new file mode 100644 index 0000000..d4254c0 --- /dev/null +++ b/new_emk_test.go @@ -0,0 +1,17 @@ +package erkkratos + +import ( + "testing" + + "github.com/orzkratos/erkkratos/internal/errors_example" + "github.com/stretchr/testify/require" + "github.com/yyle88/erero" +) + +func TestNewEmtBottle_Wrap(t *testing.T) { + emtBottle := NewEmtBottle(errors_example.ErrorServerDbError, "msg", "erk") + erk := emtBottle.Wrap(erero.New("wac")) + require.NotNil(t, erk) + require.True(t, errors_example.IsServerDbError(erk)) + t.Log(erk) +} diff --git a/new_erk.go b/new_erk.go index 5346cb3..b437851 100644 --- a/new_erk.go +++ b/new_erk.go @@ -1,43 +1,44 @@ package erkkratos -import "github.com/go-kratos/kratos/v2/errors" - -// NewErkFsK 指定错误的前缀让错误打印更加简单 -func NewErkFsK(efc func(format string, args ...interface{}) *errors.Error, caption string, middleOpt string) func(erx error) *errors.Error { - return func(erx error) *errors.Error { - return efc("%s%s%s", caption, middleOpt, erx).WithCause(erx) +import ( + "github.com/go-kratos/kratos/v2/errors" +) + +// ErkBottle 封装错误处理所需的属性 +type ErkBottle struct { + efc func(format string, args ...interface{}) *errors.Error + caption string + punctuation string +} + +// NewErkBottle 创建一个新的 ErkBottle 实例 +func NewErkBottle(efc func(format string, args ...interface{}) *errors.Error, caption string, punctuation string) *ErkBottle { + return &ErkBottle{ + efc: efc, + caption: caption, + punctuation: punctuation, } } -func NewErkFsB(efc func(format string, args ...interface{}) *errors.Error, caption string) func(erx error) *errors.Error { - return NewErkFsK(efc, caption, " ") -} - -func NewErkFsC(efc func(format string, args ...interface{}) *errors.Error, caption string) func(erx error) *errors.Error { - return NewErkFsK(efc, caption, ":") -} - -func NewErkFsE(efc func(format string, args ...interface{}) *errors.Error, caption string) func(erx error) *errors.Error { - return NewErkFsK(efc, caption, "=") -} - -// NewErkMtK 让错误返回的消息能够被前端直接展示,而把错误的细节放在 metadata 里面 -func NewErkMtK(efc func(format string, args ...interface{}) *errors.Error, message string, metaKeyName string) func(erx error) *errors.Error { - return func(erx error) *errors.Error { - return efc("%s", message).WithCause(erx).WithMetadata(map[string]string{ - metaKeyName: erx.Error(), - }) - } +// SetErkFunc 设置 efc 属性并返回自身,以支持链式调用 +func (b *ErkBottle) SetErkFunc(efc func(format string, args ...interface{}) *errors.Error) *ErkBottle { + b.efc = efc + return b } -func NewErkMtX(efc func(format string, args ...interface{}) *errors.Error, message string) func(erx error) *errors.Error { - return NewErkMtK(efc, message, "erx") +// SetCaption 设置 caption 属性并返回自身,以支持链式调用 +func (b *ErkBottle) SetCaption(caption string) *ErkBottle { + b.caption = caption + return b } -func As(erx error) (erk *errors.Error, ok bool) { - return erk, errors.As(erx, &erk) +// SetPunctuation 设置 punctuation 属性并返回自身,以支持链式调用 +func (b *ErkBottle) SetPunctuation(punctuation string) *ErkBottle { + b.punctuation = punctuation + return b } -func Is(erx error, target error) (ok bool) { - return errors.Is(erx, target) +// Wrap 方法用于包装错误并返回格式化的错误信息 +func (b *ErkBottle) Wrap(erx error) *errors.Error { + return b.efc("%s%s%s", b.caption, b.punctuation, erx).WithCause(erx) } diff --git a/new_erk_test.go b/new_erk_test.go index 99b6157..07f2b01 100644 --- a/new_erk_test.go +++ b/new_erk_test.go @@ -3,56 +3,15 @@ package erkkratos import ( "testing" - "github.com/go-kratos/kratos/v2/errors" "github.com/orzkratos/erkkratos/internal/errors_example" "github.com/stretchr/testify/require" "github.com/yyle88/erero" ) -func TestNewErkFsC(t *testing.T) { - erk := NewErkFsC(errors_example.ErrorServerDbError, "erk")(erero.New("wac")) +func TestNewErkBottle_Wrap(t *testing.T) { + erkBottle := NewErkBottle(errors_example.ErrorServerDbError, "erk", "=") + erk := erkBottle.Wrap(erero.New("wac")) require.NotNil(t, erk) require.True(t, errors_example.IsServerDbError(erk)) t.Log(erk) } - -func TestNewErkMtX(t *testing.T) { - erk := NewErkMtX(errors_example.ErrorServerDbError, "msg")(erero.New("wac")) - require.NotNil(t, erk) - require.True(t, errors_example.IsServerDbError(erk)) - t.Log(erk) -} - -func TestNewErkMtX_2(t *testing.T) { - erk := NewErkMtX(errors_example.ErrorServerDbError, "msg")(erero.New("wac")) - require.NotNil(t, erk) - require.True(t, errors_example.IsServerDbError(erk)) - t.Log(erk) - - var erx error = erk - - var target *errors.Error - ok := errors.As(erx, &target) - t.Log(ok) - t.Log(target) -} - -func TestAs(t *testing.T) { - { - var erk = errors_example.ErrorServerDbError("wrong") - var erx error = erk - res, ok := As(erx) - require.True(t, ok) - t.Log(res) - require.NotNil(t, res) - } - - { - var erk *errors.Error - var erx error = erk - res, ok := As(erx) - require.True(t, ok) - t.Log(res) - require.Nil(t, res) - } -}