2015-05-18 07:54:29 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2015-05-18 07:54:29 +02:00
|
|
|
func Recover() echo.MiddlewareFunc {
|
|
|
|
// TODO: Provide better stack trace `https://github.com/go-errors/errors` `https://github.com/docker/libcontainer/tree/master/stacktrace`
|
|
|
|
return func(h echo.HandlerFunc) echo.HandlerFunc {
|
2015-05-20 23:38:51 +02:00
|
|
|
return func(c *echo.Context) error {
|
2015-05-18 07:54:29 +02:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
trace := make([]byte, 1<<16)
|
|
|
|
n := runtime.Stack(trace, true)
|
2015-11-21 05:13:22 +02:00
|
|
|
c.Error(fmt.Errorf("panic recover\n %v\n stack trace %d bytes\n %s",
|
2015-05-20 23:38:51 +02:00
|
|
|
err, n, trace[:n]))
|
2015-05-18 07:54:29 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return h(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|