forked from labstack/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recover.go
39 lines (35 loc) · 875 Bytes
/
recover.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
package middleware
import (
"errors"
"github.com/labstack/echo"
)
type (
RecoverOptions struct {
}
)
// Recover returns a middleware which recovers from panics anywhere in the chain
// and handles the control to the centralized HTTPErrorHandler.
func Recover(options ...*RecoverOptions) echo.MiddlewareFunc {
return func(next echo.Handler) echo.Handler {
// TODO: Provide better stack trace
// - `https://github.com/go-errors/errors`
// - `https://github.com/docker/libcontainer/tree/master/stacktrace`
return echo.HandlerFunc(func(c echo.Context) error {
defer func() {
if r := recover(); r != nil {
e := ""
switch r := r.(type) {
case string:
e = r
case error:
e = r.Error()
default:
e = "unknown error"
}
c.Error(errors.New("panic recover|" + e))
}
}()
return next.Handle(c)
})
}
}