蛋蛋星球 后台端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validate_comm.go 933 B

2 週之前
123456789101112131415161718192021222324252627282930313233
  1. package validate
  2. import (
  3. "applet/app/e"
  4. "applet/app/utils"
  5. "applet/app/utils/logx"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/go-playground/validator/v10"
  9. )
  10. func HandleValidateErr(err error) error {
  11. switch err.(type) {
  12. case *json.UnmarshalTypeError:
  13. return e.NewErr(e.ERR_UNMARSHAL, "参数格式错误")
  14. case validator.ValidationErrors:
  15. errs := err.(validator.ValidationErrors)
  16. transMsgMap := errs.Translate(utils.ValidatorTrans) // utils.ValidatorTrans \app\utils\validator_err_trans.go::ValidatorTransInit初始化获得
  17. transMsgOne := transMsgMap[GetOneKeyOfMapString(transMsgMap)]
  18. return e.NewErr(e.ERR_INVALID_ARGS, transMsgOne)
  19. default:
  20. _ = logx.Error(err)
  21. return e.NewErr(e.ERR, fmt.Sprintf("validate request params, err:%v\n", err))
  22. }
  23. }
  24. // GetOneKeyOfMapString 取出Map的一个key
  25. func GetOneKeyOfMapString(collection map[string]string) string {
  26. for k := range collection {
  27. return k
  28. }
  29. return ""
  30. }