错误消息

This commit is contained in:
yun
2025-10-26 20:32:02 +08:00
parent 5c951132d9
commit 5b6002c688
11 changed files with 319 additions and 110 deletions
+72 -110
View File
@@ -1,135 +1,97 @@
package errorx
import (
"encoding/json"
"errors"
"context"
"google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
"github.com/yuninks/langx"
)
const defaultCode = 400
// Key生成错误信息
type Errorx struct {
list []CodeError
type LangError interface {
Copy() LangError // 复制一个新的错误信息
Error() string // 实现error接口&获取翻译后的错误信息
GetCode() int // 获取翻译后的Code
GetKey() string // 获取原Key值
GetFormat() map[string]string // 获取附加数据
SetFormat(format map[string]string) // 设置附加数据
SetCtx(ctxHttp context.Context) // 设置上下文
SetLang(lang string) // 设置语言
SetFormatKV(key, value string) // 设置附加数据键值对
}
type CodeError struct {
*status.Status
// Code int `json:"code"`
// Message string `json:"message"`
type langError struct {
ctx context.Context
key string
format map[string]string
}
func NewCodeError(code int, msg string) error {
return &Errorx{
list: []CodeError{
{
status.New(codes.Code(code), msg),
// Code: code,
// Message: msg,
},
},
func (l *langError) Copy() LangError {
return &langError{
ctx: l.ctx,
key: l.key,
format: l.format,
}
}
func New(msg string) error {
return NewCodeError(defaultCode, msg)
func (e *langError) SetFormat(format map[string]string) {
e.format = format
}
func WithError(err error, msg string) error {
if err == nil {
return NewCodeError(defaultCode, msg)
func (l *langError) SetFormatKV(key, value string) {
if l.format == nil {
l.format = make(map[string]string)
}
e, ok := err.(*Errorx)
if ok {
e.list = append(e.list, CodeError{
status.New(codes.Code(defaultCode), msg),
// Code: defaultCode,
// Message: msg,
})
return e
} else {
return &Errorx{
list: []CodeError{
{
status.New(codes.Code(defaultCode), msg),
// Code: defaultCode,
// Message: err.Error(),
},
{
status.New(codes.Code(defaultCode), msg),
// Code: defaultCode,
// Message: msg,
},
},
}
l.format[key] = value
}
func (e *langError) SetCtx(ctxHttp context.Context) {
e.ctx = ctxHttp
}
func (l *langError) Error() string {
errLang := langx.GetCtxLang(l.ctx)
return langx.GetFormat(errLang, l.key, l.format)
}
func (e *langError) GetCode() int {
return langx.GetCode(e.key)
}
func (e *langError) GetKey() string {
return e.key
}
func (e *langError) GetFormat() map[string]string {
if e.format == nil {
e.format = make(map[string]string)
}
return e.format
}
func (e *langError) SetLang(lang string) {
e.ctx = langx.SetCtxLang(e.ctx, lang)
}
func NewErrorf(ctx context.Context, key string, format map[string]string) error {
return &langError{
ctx: ctx,
key: key,
format: format,
}
}
func (e *Errorx) Error() string {
c := len(e.list)
if c == 0 {
return ""
func NewError(ctx context.Context, key string) error {
return &langError{
ctx: ctx,
key: key,
}
return e.list[c-1].Message()
}
func (e *Errorx) Code() int {
c := len(e.list)
if c == 0 {
return 200
func NewStruct(ctx context.Context, key string, format map[string]string) LangError {
return &langError{
ctx: ctx,
key: key,
format: format,
}
return int(e.list[c-1].Code())
}
func FromError(err error) (s *status.Status, ok bool) {
if err == nil {
return nil, true
}
type grpcstatus interface{ GRPCStatus() *status.Status }
if gs, ok := err.(grpcstatus); ok {
if gs.GRPCStatus() == nil {
// Error has status nil, which maps to codes.OK. There
// is no sensible behavior for this, so we turn it into
// an error with codes.Unknown and discard the existing
// status.
return status.New(codes.Unknown, err.Error()), false
}
return gs.GRPCStatus(), true
}
var gs grpcstatus
if errors.As(err, &gs) {
if gs.GRPCStatus() == nil {
// Error wraps an error that has status nil, which maps
// to codes.OK. There is no sensible behavior for this,
// so we turn it into an error with codes.Unknown and
// discard the existing status.
return status.New(codes.Unknown, err.Error()), false
}
p := gs.GRPCStatus().Proto()
p.Message = err.Error()
return status.FromProto(p), true
}
return status.New(codes.Unknown, err.Error()), false
}
func (e *Errorx) GRPCStatus() *status.Status {
c := len(e.list)
if c == 0 {
return nil
}
return e.list[c-1].Status
}
func (e *Errorx) Is(target error) bool {
return true
}
func (e *Errorx) Data() []CodeError {
return e.list
}
func (e *Errorx) String() string {
b, _ := json.Marshal(e.list)
return string(b)
}