蛋蛋星球-客户端
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.

svc_validate_common.go 859 B

1 month ago
3 weeks ago
1 month ago
3 weeks ago
1 month ago
3 weeks ago
1 month ago
123456789101112131415161718192021222324252627282930
  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. }