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-07-27 18:34:44 +02:00
|
|
|
// RecoverConfig defines the config for Recover middleware.
|
2016-03-14 22:55:38 +02:00
|
|
|
RecoverConfig struct {
|
2016-07-27 18:34:44 +02:00
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
|
|
|
|
2016-05-10 20:52:04 +02:00
|
|
|
// Size of the stack to be printed.
|
|
|
|
// Optional. Default value 4KB.
|
2016-05-19 03:53:54 +02:00
|
|
|
StackSize int `json:"stack_size"`
|
2016-03-20 00:47:20 +02:00
|
|
|
|
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.
|
2016-05-10 20:52:04 +02:00
|
|
|
// Optional. Default value false.
|
2016-05-19 03:53:54 +02:00
|
|
|
DisableStackAll bool `json:"disable_stack_all"`
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2016-04-06 03:57:57 +02:00
|
|
|
// DisablePrintStack disables printing stack trace.
|
2016-05-10 20:52:04 +02:00
|
|
|
// Optional. Default value as false.
|
2016-05-19 03:53:54 +02:00
|
|
|
DisablePrintStack bool `json:"disable_print_stack"`
|
2016-02-18 07:01:47 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-03-14 22:55:38 +02:00
|
|
|
var (
|
2016-07-27 18:34:44 +02:00
|
|
|
// DefaultRecoverConfig is the default Recover middleware config.
|
2016-03-14 22:55:38 +02:00
|
|
|
DefaultRecoverConfig = RecoverConfig{
|
2016-07-27 18:34:44 +02:00
|
|
|
Skipper: defaultSkipper,
|
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-09-01 05:10:14 +02:00
|
|
|
// RecoverWithConfig returns a Recover middleware with config.
|
2016-05-13 02:45:00 +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
|
2016-07-27 18:34:44 +02:00
|
|
|
if config.Skipper == nil {
|
|
|
|
config.Skipper = DefaultRecoverConfig.Skipper
|
|
|
|
}
|
2016-04-01 01:30:19 +02:00
|
|
|
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-07-27 18:34:44 +02:00
|
|
|
if config.Skipper(c) {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|