Skip to content

Commit

Permalink
重写代码
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyile committed Sep 30, 2024
1 parent 6d2c7c5 commit 72f9b1d
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 88 deletions.
19 changes: 19 additions & 0 deletions erk_as.go
Original file line number Diff line number Diff line change
@@ -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)
}
39 changes: 39 additions & 0 deletions erk_as_test.go
Original file line number Diff line number Diff line change
@@ -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)) //依然相等
}
16 changes: 11 additions & 5 deletions erkrequire/erk_require.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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...)
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
3 changes: 3 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package utils

func NewBoolean(b bool) bool { return b }
44 changes: 44 additions & 0 deletions new_emk.go
Original file line number Diff line number Diff line change
@@ -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(),
})
}
17 changes: 17 additions & 0 deletions new_emk_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
63 changes: 32 additions & 31 deletions new_erk.go
Original file line number Diff line number Diff line change
@@ -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)
}
47 changes: 3 additions & 44 deletions new_erk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 72f9b1d

Please sign in to comment.