-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
main.go
42 lines (32 loc) · 795 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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)
}