iris/_examples/routing/http-errors/reset-body/main.go
Gerasimos (Makis) Maropoulos 802348cedd fix https://github.com/kataras/iris/issues/1569#issuecomment-663739177
Former-commit-id: c55f1023f4d93f6712c7fa4d299bcf1098872ecf
2020-07-25 09:23:34 +03:00

43 lines
795 B
Go

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)
}