1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 18:37:05 +00:00

More on Transactions: Fallback on, unexpected, panics and able to send 'silent' error which stills reverts the changes but no output

This commit is contained in:
Gerasimos (Makis) Maropoulos
2016-12-18 14:08:28 +02:00
parent 1da8231abd
commit 88c98bb1e1
5 changed files with 129 additions and 19 deletions

View File

@@ -972,5 +972,69 @@ func TestTransactionsMiddleware(t *testing.T) {
ContentType("text/html", api.Config.Charset).
Body().
Equal(expectedResponse)
}
func TestTransactionFailureCompletionButSilently(t *testing.T) {
iris.ResetDefault()
expectedBody := "I don't care for any unexpected panics, this response should be sent."
iris.Get("/panic_silent", func(ctx *iris.Context) {
ctx.BeginTransaction(func(scope *iris.TransactionScope) {
scope.Context.Write("blablabla this should not be shown because of 'unexpected' panic.")
panic("OMG, UNEXPECTED ERROR BECAUSE YOU ARE NOT A DISCIPLINED PROGRAMMER, BUT IRIS HAS YOU COVERED!")
})
ctx.WriteString(expectedBody)
})
iris.Get("/expected_error_but_silent_instead_of_send_the_reason", func(ctx *iris.Context) {
ctx.BeginTransaction(func(scope *iris.TransactionScope) {
scope.Context.Write("this will not be sent.")
// complete with a failure ( so revert the changes) but do it silently.
scope.Complete(iris.NewErrFallback())
})
ctx.WriteString(expectedBody)
})
iris.Get("/silly_way_expected_error_but_silent_instead_of_send_the_reason", func(ctx *iris.Context) {
ctx.BeginTransaction(func(scope *iris.TransactionScope) {
scope.Context.Write("this will not be sent.")
// or if you know the error will be silent from the beggining: err := &iris.ErrFallback{}
err := iris.NewErrWithStatus()
fail := true
if fail {
err.Status(iris.StatusBadRequest).Reason("we dont know but it was expected error")
}
// we change our mind we don't want to send the error to the user, so err.Silent to the .Complete
// complete with a failure ( so revert the changes) but do it silently.
scope.Complete(err.Silent())
})
ctx.WriteString(expectedBody)
})
e := httptest.New(iris.Default, t)
e.GET("/panic_silent").Expect().
Status(iris.StatusOK).
Body().
Equal(expectedBody)
e.GET("/expected_error_but_silent_instead_of_send_the_reason").
Expect().
Status(iris.StatusOK).
Body().
Equal(expectedBody)
e.GET("/silly_way_expected_error_but_silent_instead_of_send_the_reason").
Expect().
Status(iris.StatusOK).
Body().
Equal(expectedBody)
}