2015-05-18 07:54:29 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
2016-02-18 07:01:47 +02:00
|
|
|
type (
|
2016-02-18 17:00:51 +02:00
|
|
|
RecoverOptions struct {
|
2016-02-18 07:01:47 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-05-18 07:54:29 +02:00
|
|
|
// Recover returns a middleware which recovers from panics anywhere in the chain
|
2015-06-08 04:57:49 +02:00
|
|
|
// and handles the control to the centralized HTTPErrorHandler.
|
2016-02-18 17:00:51 +02:00
|
|
|
func Recover(options ...*RecoverOptions) echo.MiddlewareFunc {
|
2016-02-19 08:50:40 +02:00
|
|
|
return func(next echo.Handler) echo.Handler {
|
2016-02-18 07:49:31 +02:00
|
|
|
// 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 err := recover(); err != nil {
|
|
|
|
trace := make([]byte, 1<<16)
|
|
|
|
n := runtime.Stack(trace, true)
|
|
|
|
c.Error(fmt.Errorf("panic recover\n %v\n stack trace %d bytes\n %s",
|
|
|
|
err, n, trace[:n]))
|
|
|
|
}
|
|
|
|
}()
|
2016-02-19 08:50:40 +02:00
|
|
|
return next.Handle(c)
|
2016-02-18 07:49:31 +02:00
|
|
|
})
|
|
|
|
}
|
2015-05-18 07:54:29 +02:00
|
|
|
}
|