蛋蛋星球-客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

31 行
859 B

  1. package svc
  2. import (
  3. "applet/app/e"
  4. "applet/app/utils"
  5. "applet/app/utils/logx"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "github.com/go-playground/validator/v10"
  10. )
  11. // HandleValidateErr 通用请求参数错误处理
  12. func HandleValidateErr(err error) error {
  13. var unmarshalTypeError *json.UnmarshalTypeError
  14. var validationErrors validator.ValidationErrors
  15. switch {
  16. case errors.As(err, &unmarshalTypeError):
  17. return e.NewErr(e.ERR_UNMARSHAL, "参数格式错误")
  18. case errors.As(err, &validationErrors):
  19. var errs validator.ValidationErrors
  20. errors.As(err, &errs)
  21. transMsgMap := errs.Translate(utils.ValidatorTrans)
  22. transMsgOne := transMsgMap[utils.GetOneKeyOfMapString(transMsgMap)]
  23. return e.NewErr(e.ERR_INVALID_ARGS, transMsgOne)
  24. default:
  25. _ = logx.Error(err)
  26. return e.NewErr(e.ERR, fmt.Sprintf("validate request params, err:%v\n", err))
  27. }
  28. }