修改错误接口的方法

This commit is contained in:
Yun
2024-07-31 16:47:23 +08:00
parent ea8d17943d
commit 8d3bd7cade
+9 -9
View File
@@ -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)
}