diff --git a/error/handler.go b/error/handler.go index 1ce5e62..b475453 100644 --- a/error/handler.go +++ b/error/handler.go @@ -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)) diff --git a/error/handler_test.go b/error/handler_test.go index d6f9143..75d9ad3 100644 --- a/error/handler_test.go +++ b/error/handler_test.go @@ -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()