Fix error handler
The errors can not be of the type validation error, for example if the json string cannot be parsed.
This commit is contained in:
parent
edec140c82
commit
74d80765e5
|
|
@ -27,7 +27,13 @@ func Handler() gin.HandlerFunc {
|
|||
for _, e := range c.Errors {
|
||||
switch e.Type {
|
||||
case gin.ErrorTypeBind:
|
||||
errs := e.Err.(validator.ValidationErrors)
|
||||
errs, ok := e.Err.(validator.ValidationErrors)
|
||||
|
||||
if !ok {
|
||||
writeError(c, e.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var stringErrors []string
|
||||
for _, err := range errs {
|
||||
stringErrors = append(stringErrors, validationErrorToText(err))
|
||||
|
|
|
|||
|
|
@ -22,6 +22,17 @@ func TestDefaultErrorInternal(t *testing.T) {
|
|||
assertJSONResponse(t, rec, 500, `{"errorCode":500, "errorDescription":"something went wrong", "error":"Internal Server Error"}`)
|
||||
}
|
||||
|
||||
func TestBindingErrorDefault(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
rec := httptest.NewRecorder()
|
||||
ctx, _ := gin.CreateTestContext(rec)
|
||||
ctx.AbortWithError(400, errors.New("you need todo something")).SetType(gin.ErrorTypeBind)
|
||||
|
||||
Handler()(ctx)
|
||||
|
||||
assertJSONResponse(t, rec, 400, `{"errorCode":400, "errorDescription":"you need todo something", "error":"Bad Request"}`)
|
||||
}
|
||||
|
||||
func TestDefaultErrorBadRequest(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
rec := httptest.NewRecorder()
|
||||
|
|
|
|||
Loading…
Reference in New Issue