Skip to content

Commit

Permalink
再次修改逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyile committed Aug 17, 2024
1 parent 4201830 commit adec07b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
22 changes: 18 additions & 4 deletions erkfc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
11 changes: 9 additions & 2 deletions erkfc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions erkrequire/erkrequire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit adec07b

Please sign in to comment.