package svc import ( "applet/app/e" "applet/app/utils" "applet/app/utils/logx" "encoding/json" "errors" "fmt" "github.com/go-playground/validator/v10" ) // HandleValidateErr 通用请求参数错误处理 func HandleValidateErr(err error) error { var unmarshalTypeError *json.UnmarshalTypeError var validationErrors validator.ValidationErrors switch { case errors.As(err, &unmarshalTypeError): return e.NewErr(e.ERR_UNMARSHAL, "参数格式错误") case errors.As(err, &validationErrors): var errs validator.ValidationErrors errors.As(err, &errs) transMsgMap := errs.Translate(utils.ValidatorTrans) transMsgOne := transMsgMap[utils.GetOneKeyOfMapString(transMsgMap)] return e.NewErr(e.ERR_INVALID_ARGS, transMsgOne) default: _ = logx.Error(err) return e.NewErr(e.ERR, fmt.Sprintf("validate request params, err:%v\n", err)) } }