chore: init bj_power monorepo (dashboard/mes/wms/wms_client/workstation), clear subproject git metadata and align naming to folder names
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package xhttp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"bj_power_mes/common/errorx"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/iancoleman/strcase"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type Body struct {
|
||||
Code uint32 `json:"code"`
|
||||
Msg string `json:"message"`
|
||||
Fields []FieldError `json:"fields,omitempty"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
type FieldError struct {
|
||||
Field string `json:"field"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
func OkJsonCtx(_ context.Context, data any) any {
|
||||
return Body{Msg: "OK", Data: data}
|
||||
}
|
||||
|
||||
func ErrorCtx(ctx context.Context, err error) (int, any) {
|
||||
logx.WithContext(ctx).Errorf("[API-ERR] %v", err)
|
||||
var errs validator.ValidationErrors
|
||||
if errors.As(err, &errs) { // �数验�错误
|
||||
var fields []FieldError
|
||||
for _, er := range errs {
|
||||
fields = append(fields, FieldError{
|
||||
Field: strcase.ToLowerCamel(er.StructField()),
|
||||
Error: er.Translate(translator),
|
||||
})
|
||||
}
|
||||
body := Body{Code: errorx.RequestParamInvalidWithField, Msg: errorx.MapErrMsg(errorx.RequestParamInvalid), Fields: fields}
|
||||
return 200, body
|
||||
}
|
||||
var fes errorx.ServiceFieldErrors
|
||||
if errors.As(err, &fes) { // 业务验�错误
|
||||
es := fes.Errors()
|
||||
if len(es) > 0 {
|
||||
fieldErrors := make([]FieldError, len(es))
|
||||
for i, err := range es {
|
||||
var e errorx.ServiceFieldError
|
||||
errors.As(err, &e)
|
||||
fieldErrors[i].Field = e.Field()
|
||||
fieldErrors[i].Error = e.Error()
|
||||
}
|
||||
body := Body{Code: errorx.RequestParamInvalidWithField, Msg: errorx.MapErrMsg(errorx.RequestParamInvalid), Fields: fieldErrors}
|
||||
return 200, body
|
||||
}
|
||||
return 200, Body{Code: errorx.RequestParamInvalid, Msg: errorx.MapErrMsg(errorx.RequestParamInvalid)}
|
||||
}
|
||||
var fe errorx.ServiceFieldError
|
||||
if errors.As(err, &fe) { // 业务验�错误
|
||||
fieldErrors := make([]FieldError, 1)
|
||||
fieldErrors[0].Field = fe.Field()
|
||||
fieldErrors[0].Error = fe.Error()
|
||||
body := Body{Code: fe.Code(), Msg: errorx.MapErrMsg(fe.Code()), Fields: fieldErrors}
|
||||
return 200, body
|
||||
}
|
||||
var ce errorx.ServiceError
|
||||
if errors.As(err, &ce) {
|
||||
body := Body{Code: ce.Code(), Msg: errorx.MapErrMsg(ce.Code(), ce.Error())}
|
||||
return 200, body
|
||||
}
|
||||
|
||||
// 未识别的普通错误(å¦?EventLoop 返回的业务错误):Msg 直接é€�å‡ºçœŸå®žåŽŸå› ï¼?
|
||||
// é�¿å…�å‰�端å�ªçœ‹åˆ°ç¬¼ç»Ÿçš„"æœ�务器开å°�å·®"è€Œæ— æ³•å®šä½�é—®é¢?
|
||||
body := Body{Code: errorx.CommonErrCode, Msg: err.Error()}
|
||||
return 200, body
|
||||
}
|
||||
Reference in New Issue
Block a user