-
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
Aug 28, 2024
1 parent
b96a52f
commit 846e269
Showing
6 changed files
with
93 additions
and
79 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,31 @@ | ||
package erkrequire | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-kratos/kratos/v2/errors" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func No(t *testing.T, erk *errors.Error, msgAndArgs ...interface{}) { | ||
require.NoError(t, eke(erk), msgAndArgs...) | ||
} | ||
|
||
func Eo(t *testing.T, erk *errors.Error, msgAndArgs ...interface{}) { | ||
require.Error(t, eke(erk), msgAndArgs...) //这里必须传递个空才行,跟前面的情况相同 | ||
} | ||
|
||
func eke(erk *errors.Error) error { | ||
if erk == nil { | ||
return nil //这里必须做这样的转换,因为两个 nil 是不一样的 | ||
} | ||
return erk | ||
} | ||
|
||
func Is(t *testing.T, 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...) | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,43 @@ | ||
package erkkratos | ||
|
||
import "github.com/go-kratos/kratos/v2/errors" | ||
|
||
// NewErkFsK 指定错误的前缀让错误打印更加简单 | ||
func NewErkFsK(efc func(format string, args ...interface{}) *errors.Error, startWith string, middleOpt string) func(erx error) *errors.Error { | ||
return func(erx error) *errors.Error { | ||
return efc("%s%s%s", startWith, middleOpt, erx).WithCause(erx) | ||
} | ||
} | ||
|
||
func NewErkFsB(efc func(format string, args ...interface{}) *errors.Error, startWith string) func(erx error) *errors.Error { | ||
return NewErkFsK(efc, startWith, " ") | ||
} | ||
|
||
func NewErkFsC(efc func(format string, args ...interface{}) *errors.Error, startWith string) func(erx error) *errors.Error { | ||
return NewErkFsK(efc, startWith, ":") | ||
} | ||
|
||
func NewErkFsE(efc func(format string, args ...interface{}) *errors.Error, startWith string) func(erx error) *errors.Error { | ||
return NewErkFsK(efc, startWith, "=") | ||
} | ||
|
||
// 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(), | ||
}) | ||
} | ||
} | ||
|
||
func NewErkMtX(efc func(format string, args ...interface{}) *errors.Error, message string) func(erx error) *errors.Error { | ||
return NewErkMtK(efc, message, "erx") | ||
} | ||
|
||
func As(erx error) (erk *errors.Error, ok bool) { | ||
return erk, errors.As(erx, &erk) | ||
} | ||
|
||
func Is(erx error, target error) (ok bool) { | ||
return errors.Is(erx, 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