1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 02:17:05 +00:00
Former-commit-id: c55f1023f4d93f6712c7fa4d299bcf1098872ecf
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-07-25 09:23:34 +03:00
parent e70ceba051
commit 802348cedd
5 changed files with 68 additions and 1 deletions

View File

@@ -0,0 +1,42 @@
package main
import (
"fmt"
"github.com/kataras/iris/v12"
)
func main() {
app := newApp()
app.Listen(":8080")
}
func newApp() *iris.Application {
app := iris.New()
app.Use(iris.Compression)
app.OnAnyErrorCode(onErrorCode)
app.Get("/", handler)
app.Configure(iris.WithResetOnFireErrorCode)
return app
}
// This is the default error handler Iris uses for any error codes.
func onErrorCode(ctx iris.Context) {
if err := ctx.GetErr(); err != nil {
ctx.WriteString(err.Error())
} else {
ctx.WriteString(iris.StatusText(ctx.GetStatusCode()))
}
}
func handler(ctx iris.Context) {
ctx.Record()
ctx.WriteString("This should NOT be written")
// [....something bad happened after we "write"]
err := fmt.Errorf("custom error")
ctx.StopWithError(iris.StatusBadRequest, err)
}

View File

@@ -0,0 +1,14 @@
package main
import (
"testing"
"github.com/kataras/iris/v12/httptest"
)
func TestResetCompressionAndFireError(t *testing.T) { // #1569
app := newApp()
e := httptest.New(t, app)
e.GET("/").Expect().Status(httptest.StatusBadRequest).Body().Equal("custom error")
}