diff --git a/hero/handler.go b/hero/handler.go index dc527d5b..9180ef03 100644 --- a/hero/handler.go +++ b/hero/handler.go @@ -118,7 +118,12 @@ func makeHandler(fn interface{}, c *Container, paramsCount int) context.Handler // } c.GetErrorHandler(ctx).HandleError(ctx, err) - return + // return [13 Sep 2020, commented that in order to be able to + // give end-developer the option not only to handle the error + // but to skip it if necessary, example: + // read form, unknown field, continue without StopWith, + // the binder should bind the method's input argument and continue + // without errors. See `mvc.TestErrorHandlerContinue` test.] } // If ~an error status code is set or~ execution has stopped diff --git a/hero/handler_test.go b/hero/handler_test.go index f3923b25..2f240d58 100644 --- a/hero/handler_test.go +++ b/hero/handler_test.go @@ -137,6 +137,16 @@ func TestPayloadBinding(t *testing.T) { return input.Username }) + h.GetErrorHandler = func(iris.Context) ErrorHandler { + return ErrorHandlerFunc(func(ctx iris.Context, err error) { + if iris.IsErrPath(err) { + return // continue. + } + + ctx.StopWithError(iris.StatusBadRequest, err) + }) + } + app := iris.New() app.Get("/", ptrHandler) app.Post("/", ptrHandler) @@ -152,6 +162,9 @@ func TestPayloadBinding(t *testing.T) { e.POST("/").WithFormField("username", "makis").Expect().Status(httptest.StatusOK).Body().Equal("makis") // FORM (multipart) e.POST("/").WithMultipart().WithFormField("username", "makis").Expect().Status(httptest.StatusOK).Body().Equal("makis") + // FORM: test ErrorHandler skip the ErrPath. + e.POST("/").WithMultipart().WithFormField("username", "makis").WithFormField("unknown", "continue"). + Expect().Status(httptest.StatusOK).Body().Equal("makis") // POST URL query. e.POST("/").WithQuery("username", "makis").Expect().Status(httptest.StatusOK).Body().Equal("makis") diff --git a/mvc/controller_test.go b/mvc/controller_test.go index 99ab24f0..0436ac5e 100644 --- a/mvc/controller_test.go +++ b/mvc/controller_test.go @@ -2,6 +2,7 @@ package mvc_test import ( + "fmt" "testing" "github.com/kataras/iris/v12" @@ -707,3 +708,36 @@ func TestControllerMethodHandlerBindStruct(t *testing.T) { e.POST("/data/42/slicetypeptr").WithJSON(manyData).Expect().Status(httptest.StatusOK).JSON().Equal(manyData) // more tests inside the hero package itself. } + +func TestErrorHandlerContinue(t *testing.T) { + app := iris.New() + m := New(app) + m.Handle(new(testControllerErrorHandlerContinue)) + + e := httptest.New(t, app) + + e.POST("/test").WithMultipart(). + WithFormField("username", "makis"). + WithFormField("age", "27"). + WithFormField("unknown", "continue"). + Expect().Status(httptest.StatusOK).Body().Equal("makis is 27 years old\n") +} + +type testControllerErrorHandlerContinue struct{} + +type registerForm struct { + Username string `form:"username"` + Age int `form:"age"` +} + +func (c *testControllerErrorHandlerContinue) HandleError(ctx iris.Context, err error) { + if iris.IsErrPath(err) { + return // continue. + } + + ctx.StopWithError(iris.StatusBadRequest, err) +} + +func (c *testControllerErrorHandlerContinue) PostTest(form registerForm) string { + return fmt.Sprintf("%s is %d years old\n", form.Username, form.Age) +}