包装一下校验错误的类型,用于区别用户输入错误和系统错误
This commit is contained in:
		
							parent
							
								
									ceabec997c
								
							
						
					
					
						commit
						7940dd5237
					
				|  | @ -11,7 +11,7 @@ type ValidError struct { | |||
| 	ErrString string | ||||
| } | ||||
| 
 | ||||
| func (e *ValidError) Error() string { | ||||
| func (e ValidError) Error() string { | ||||
| 	return e.ErrString | ||||
| } | ||||
| 
 | ||||
|  | @ -32,7 +32,11 @@ func ShouldBindWith(req *http.Request, obj interface{}, b binding.Binding) error | |||
| 		// 非validator.ValidationErrors类型错误直接返回
 | ||||
| 		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 { | ||||
| 	return ShouldBindWith(req, obj, binding.JSON) | ||||
|  |  | |||
|  | @ -86,7 +86,7 @@ type TransValidError struct { | |||
| func (e TransValidError) Error() string { | ||||
| 	return e.ErrorString | ||||
| } | ||||
| func (ve ValidationErrors) Translate(ut ut.Translator) TransValidError { | ||||
| func (ve ValidationErrors) Translate(ut ut.Translator) error { | ||||
| 	var result TransValidError | ||||
| 	var fe *fieldError | ||||
| 	if len(ve) == 0 { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue