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:
42
_examples/routing/http-errors/reset-body/main.go
Normal file
42
_examples/routing/http-errors/reset-body/main.go
Normal 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)
|
||||
}
|
||||
14
_examples/routing/http-errors/reset-body/main_test.go
Normal file
14
_examples/routing/http-errors/reset-body/main_test.go
Normal 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")
|
||||
}
|
||||
Reference in New Issue
Block a user