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