2015-05-18 07:54:29 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2016-03-14 22:55:38 +02:00
|
|
|
"fmt"
|
|
|
|
"runtime"
|
2015-05-18 07:54:29 +02:00
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
2016-03-14 22:55:38 +02:00
|
|
|
"github.com/labstack/gommon/color"
|
2015-05-18 07:54:29 +02:00
|
|
|
)
|
|
|
|
|
2016-02-18 07:01:47 +02:00
|
|
|
type (
|
2016-04-01 01:30:19 +02:00
|
|
|
// RecoverConfig defines the config for recover middleware.
|
2016-03-14 22:55:38 +02:00
|
|
|
RecoverConfig struct {
|
2016-03-20 00:47:20 +02:00
|
|
|
// StackSize is the stack size to be printed.
|
2016-04-06 03:57:57 +02:00
|
|
|
// Optional with default value as 4 KB.
|
2016-03-20 00:47:20 +02:00
|
|
|
StackSize int
|
|
|
|
|
2016-04-06 03:57:57 +02:00
|
|
|
// DisableStackAll disables formatting stack traces of all other goroutines
|
|
|
|
// into buffer after the trace for the current goroutine.
|
|
|
|
// Optional with default value as false.
|
|
|
|
DisableStackAll bool
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-04-06 03:57:57 +02:00
|
|
|
// DisablePrintStack disables printing stack trace.
|
|
|
|
// Optional with default value as false.
|
|
|
|
DisablePrintStack bool
|
2016-02-18 07:01:47 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-03-14 22:55:38 +02:00
|
|
|
var (
|
2016-03-20 00:47:20 +02:00
|
|
|
// DefaultRecoverConfig is the default recover middleware config.
|
2016-03-14 22:55:38 +02:00
|
|
|
DefaultRecoverConfig = RecoverConfig{
|
2016-04-06 03:57:57 +02:00
|
|
|
StackSize: 4 << 10, // 4 KB
|
|
|
|
DisableStackAll: false,
|
|
|
|
DisablePrintStack: false,
|
2016-03-14 22:55:38 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Recover returns a middleware which recovers from panics anywhere in the chain
|
|
|
|
// and handles the control to the centralized HTTPErrorHandler.
|
2016-03-14 22:55:38 +02:00
|
|
|
func Recover() echo.MiddlewareFunc {
|
2016-04-08 06:20:50 +02:00
|
|
|
return RecoverWithConfig(DefaultRecoverConfig)
|
2016-03-14 22:55:38 +02:00
|
|
|
}
|
|
|
|
|
2016-04-08 06:20:50 +02:00
|
|
|
// RecoverWithConfig returns a recover middleware from config.
|
2016-03-20 00:47:20 +02:00
|
|
|
// See `Recover()`.
|
2016-04-08 06:20:50 +02:00
|
|
|
func RecoverWithConfig(config RecoverConfig) echo.MiddlewareFunc {
|
2016-04-01 01:30:19 +02:00
|
|
|
// Defaults
|
|
|
|
if config.StackSize == 0 {
|
|
|
|
config.StackSize = DefaultRecoverConfig.StackSize
|
|
|
|
}
|
|
|
|
|
2016-04-02 23:19:39 +02:00
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2016-02-18 07:49:31 +02:00
|
|
|
defer func() {
|
2016-03-11 02:35:20 +02:00
|
|
|
if r := recover(); r != nil {
|
2016-03-14 22:55:38 +02:00
|
|
|
var err error
|
2016-03-11 02:35:20 +02:00
|
|
|
switch r := r.(type) {
|
|
|
|
case error:
|
2016-03-14 22:55:38 +02:00
|
|
|
err = r
|
2016-03-11 02:35:20 +02:00
|
|
|
default:
|
2016-03-14 22:55:38 +02:00
|
|
|
err = fmt.Errorf("%v", r)
|
|
|
|
}
|
|
|
|
stack := make([]byte, config.StackSize)
|
2016-04-06 03:57:57 +02:00
|
|
|
length := runtime.Stack(stack, !config.DisableStackAll)
|
|
|
|
if !config.DisablePrintStack {
|
2016-03-19 17:38:35 +02:00
|
|
|
c.Logger().Printf("[%s] %s %s", color.Red("PANIC RECOVER"), err, stack[:length])
|
2016-03-11 02:35:20 +02:00
|
|
|
}
|
2016-03-14 22:55:38 +02:00
|
|
|
c.Error(err)
|
2016-02-18 07:49:31 +02:00
|
|
|
}
|
|
|
|
}()
|
2016-04-02 23:19:39 +02:00
|
|
|
return next(c)
|
|
|
|
}
|
2016-02-18 07:49:31 +02:00
|
|
|
}
|
2015-05-18 07:54:29 +02:00
|
|
|
}
|