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