修改错误接口的方法
This commit is contained in:
@@ -5,12 +5,12 @@ import (
|
||||
)
|
||||
|
||||
type LangError interface {
|
||||
NewError(ctx context.Context, key string) error
|
||||
NewErrorFormat(ctx context.Context, key string, format map[string]string) error
|
||||
Error() string
|
||||
GetCode() int
|
||||
GetMsg() string
|
||||
GetFormat() map[string]string
|
||||
NewError(ctx context.Context, key string) error // New错误
|
||||
NewErrorFormat(ctx context.Context, key string, format map[string]string) error // New错误
|
||||
Error() string // 实现error接口&获取翻译后的错误信息
|
||||
GetCode() int // 获取翻译后的Code
|
||||
GetKey() string // 获取原Key值
|
||||
GetFormat() map[string]string // 获取附加数据
|
||||
}
|
||||
|
||||
type langError struct {
|
||||
@@ -21,8 +21,6 @@ type langError struct {
|
||||
|
||||
type errorConst string
|
||||
|
||||
const errorLang errorConst = "errorLang"
|
||||
|
||||
func (e *langError) Error() string {
|
||||
errLang := e.ctx.Value(errorLang)
|
||||
l := ""
|
||||
@@ -36,7 +34,7 @@ func (e *langError) GetCode() int {
|
||||
return GetCode(e.key)
|
||||
}
|
||||
|
||||
func (e *langError) GetMsg() string {
|
||||
func (e *langError) GetKey() string {
|
||||
return e.key
|
||||
}
|
||||
|
||||
@@ -62,6 +60,8 @@ func NewError(ctx context.Context, key string) error {
|
||||
}
|
||||
}
|
||||
|
||||
const errorLang errorConst = "errorLang"
|
||||
|
||||
func SetCtxLang(ctx context.Context, lang string) context.Context {
|
||||
return context.WithValue(ctx, errorLang, lang)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user