1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00
echo/middleware/recover.go
Vishal Rana 19d51dad4c Static as middleware
Signed-off-by: Vishal Rana <vr@labstack.com>
2016-02-18 22:50:40 -08:00

34 lines
843 B
Go

package middleware
import (
"fmt"
"runtime"
"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 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]))
}
}()
return next.Handle(c)
})
}
}