-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yangyile
committed
Sep 30, 2024
1 parent
6d2c7c5
commit 72f9b1d
Showing
10 changed files
with
176 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) //依然相等 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package utils | ||
|
||
func NewBoolean(b bool) bool { return b } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters