-
-
Notifications
You must be signed in to change notification settings - Fork 176
/
main.go
64 lines (50 loc) · 1.17 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"errors"
"fmt"
"github.com/kataras/iris/v12"
// "github.com/kataras/iris/v12/context"
)
func main() {
app := iris.New()
app.UseRouter(iris.Compression)
app.UseRouter(myErrorHandler)
app.Get("/", handler)
app.Listen(":8080")
}
func myErrorHandler(ctx iris.Context) {
recorder := ctx.Recorder()
defer func() {
var err error
if v := recover(); v != nil { // panic
if panicErr, ok := v.(error); ok {
err = panicErr
} else {
err = errors.New(fmt.Sprint(v))
}
} else { // custom error.
err = ctx.GetErr()
}
if err != nil {
// To keep compression after reset:
// clear body and any headers created between recorder and handler.
recorder.ResetBody()
recorder.ResetHeaders()
//
// To disable compression after reset:
// recorder.Reset()
// recorder.ResponseWriter.(*context.CompressResponseWriter).Disabled = true
//
ctx.StopWithJSON(iris.StatusInternalServerError, iris.Map{
"message": err.Error(),
})
}
}()
ctx.Next()
}
func handler(ctx iris.Context) {
ctx.WriteString("Content may fall")
ctx.Header("X-Test", "value")
// ctx.SetErr(fmt.Errorf("custom error message"))
panic("errr!")
}