|
123456789101112131415161718192021222324252627282930313233 |
- package validate
-
- import (
- "applet/app/e"
- "applet/app/utils"
- "applet/app/utils/logx"
- "encoding/json"
- "fmt"
- "github.com/go-playground/validator/v10"
- )
-
- func HandleValidateErr(err error) error {
- switch err.(type) {
- case *json.UnmarshalTypeError:
- return e.NewErr(e.ERR_UNMARSHAL, "参数格式错误")
- case validator.ValidationErrors:
- errs := err.(validator.ValidationErrors)
- transMsgMap := errs.Translate(utils.ValidatorTrans) // utils.ValidatorTrans \app\utils\validator_err_trans.go::ValidatorTransInit初始化获得
- transMsgOne := transMsgMap[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))
- }
- }
-
- // GetOneKeyOfMapString 取出Map的一个key
- func GetOneKeyOfMapString(collection map[string]string) string {
- for k := range collection {
- return k
- }
- return ""
- }
|