广告平台(站长下代理使用)
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 725 B

4 months ago
1234567891011121314151617181920212223242526
  1. package svc
  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. // HandleValidateErr 通用请求参数错误处理
  11. func HandleValidateErr(err error) error {
  12. switch err.(type) {
  13. case *json.UnmarshalTypeError:
  14. return e.NewErr(e.ERR_UNMARSHAL, "参数格式错误")
  15. case validator.ValidationErrors:
  16. errs := err.(validator.ValidationErrors)
  17. transMsgMap := errs.Translate(utils.ValidatorTrans)
  18. transMsgOne := transMsgMap[utils.GetOneKeyOfMapString(transMsgMap)]
  19. return e.NewErr(e.ERR_INVALID_ARGS, transMsgOne)
  20. default:
  21. _ = logx.Error(err)
  22. return e.NewErr(e.ERR, fmt.Sprintf("validate request params, err:%v\n", err))
  23. }
  24. }