蛋蛋星球 后台端
 
 
 
 

31 rinda
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. }