2015-05-17 22:54:29 -07:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Recover returns a middleware which recovers from panics anywhere in the chain
|
2015-06-07 19:57:49 -07:00
|
|
|
// and handles the control to the centralized HTTPErrorHandler.
|
2016-02-08 17:26:00 -08:00
|
|
|
func Recover() echo.MiddlewareFunc {
|
2016-02-15 08:11:29 -08:00
|
|
|
return func(h echo.Handler) echo.Handler {
|
2016-02-07 23:02:37 -08:00
|
|
|
// TODO: Provide better stack trace `https://github.com/go-errors/errors` `https://github.com/docker/libcontainer/tree/master/stacktrace`
|
2016-02-15 08:11:29 -08:00
|
|
|
return echo.HandlerFunc(func(c echo.Context) error {
|
2015-05-17 22:54:29 -07:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
trace := make([]byte, 1<<16)
|
|
|
|
n := runtime.Stack(trace, true)
|
2015-11-20 19:13:22 -08:00
|
|
|
c.Error(fmt.Errorf("panic recover\n %v\n stack trace %d bytes\n %s",
|
2015-05-20 14:38:51 -07:00
|
|
|
err, n, trace[:n]))
|
2015-05-17 22:54:29 -07:00
|
|
|
}
|
|
|
|
}()
|
2016-02-08 22:17:20 -08:00
|
|
|
return h.Handle(c)
|
2016-02-15 08:11:29 -08:00
|
|
|
})
|
2015-05-17 22:54:29 -07:00
|
|
|
}
|
|
|
|
}
|