包装一下校验错误的类型,用于区别用户输入错误和系统错误

This commit is contained in:
gamife 2021-02-04 10:31:56 +08:00
parent ceabec997c
commit 7940dd5237
2 changed files with 7 additions and 3 deletions

View File

@ -11,7 +11,7 @@ type ValidError struct {
ErrString string ErrString string
} }
func (e *ValidError) Error() string { func (e ValidError) Error() string {
return e.ErrString return e.ErrString
} }
@ -32,7 +32,11 @@ func ShouldBindWith(req *http.Request, obj interface{}, b binding.Binding) error
// 非validator.ValidationErrors类型错误直接返回 // 非validator.ValidationErrors类型错误直接返回
return err return err
} }
return errs.Translate(binding.ValidTrans) err0 := errs.Translate(binding.ValidTrans)
if err0 != nil {
return ValidError{ErrString: err0.Error()}
}
return nil
} }
func ShouldBindJSON(req *http.Request, obj interface{}) error { func ShouldBindJSON(req *http.Request, obj interface{}) error {
return ShouldBindWith(req, obj, binding.JSON) return ShouldBindWith(req, obj, binding.JSON)

View File

@ -86,7 +86,7 @@ type TransValidError struct {
func (e TransValidError) Error() string { func (e TransValidError) Error() string {
return e.ErrorString return e.ErrorString
} }
func (ve ValidationErrors) Translate(ut ut.Translator) TransValidError { func (ve ValidationErrors) Translate(ut ut.Translator) error {
var result TransValidError var result TransValidError
var fe *fieldError var fe *fieldError
if len(ve) == 0 { if len(ve) == 0 {