2015-05-18 07:54:29 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2016-03-14 22:55:38 +02:00
|
|
|
"fmt"
|
2022-03-16 01:29:42 +02:00
|
|
|
"net/http"
|
2016-03-14 22:55:38 +02:00
|
|
|
"runtime"
|
2015-05-18 07:54:29 +02:00
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
"github.com/labstack/echo/v5"
|
2015-05-18 07:54:29 +02:00
|
|
|
)
|
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
// RecoverConfig defines the config for Recover middleware.
|
|
|
|
type RecoverConfig struct {
|
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
2022-01-24 12:23:41 +02:00
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
// Size of the stack to be printed.
|
|
|
|
// Optional. Default value 4KB.
|
|
|
|
StackSize int
|
2016-07-27 18:34:44 +02:00
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
// DisableStackAll disables formatting stack traces of all other goroutines
|
|
|
|
// into buffer after the trace for the current goroutine.
|
|
|
|
// Optional. Default value false.
|
|
|
|
DisableStackAll bool
|
2016-03-20 00:47:20 +02:00
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
// DisablePrintStack disables printing stack trace.
|
|
|
|
// Optional. Default value as false.
|
|
|
|
DisablePrintStack bool
|
|
|
|
}
|
2016-02-18 07:01:47 +02:00
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
// DefaultRecoverConfig is the default Recover middleware config.
|
|
|
|
var DefaultRecoverConfig = RecoverConfig{
|
|
|
|
Skipper: DefaultSkipper,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
// RecoverWithConfig returns a Recovery middleware with config or panics on invalid configuration.
|
2016-04-08 06:20:50 +02:00
|
|
|
func RecoverWithConfig(config RecoverConfig) echo.MiddlewareFunc {
|
2021-07-15 22:34:01 +02:00
|
|
|
return toMiddlewareOrPanic(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToMiddleware converts RecoverConfig to middleware or returns an error for invalid configuration
|
|
|
|
func (config RecoverConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
|
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 {
|
2021-07-15 22:34:01 +02:00
|
|
|
return func(c echo.Context) (err 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 {
|
2022-03-16 01:29:42 +02:00
|
|
|
if r == http.ErrAbortHandler {
|
|
|
|
panic(r)
|
|
|
|
}
|
2021-07-15 22:34:01 +02:00
|
|
|
tmpErr, ok := r.(error)
|
2017-09-21 21:07:04 +02:00
|
|
|
if !ok {
|
2021-07-15 22:34:01 +02:00
|
|
|
tmpErr = fmt.Errorf("%v", r)
|
2016-03-14 22:55:38 +02:00
|
|
|
}
|
2016-04-06 03:57:57 +02:00
|
|
|
if !config.DisablePrintStack {
|
2021-07-15 22:34:01 +02:00
|
|
|
stack := make([]byte, config.StackSize)
|
|
|
|
length := runtime.Stack(stack, !config.DisableStackAll)
|
|
|
|
tmpErr = fmt.Errorf("[PANIC RECOVER] %w %s", tmpErr, stack[:length])
|
2022-01-24 12:23:41 +02:00
|
|
|
}
|
2021-07-15 22:34:01 +02:00
|
|
|
err = tmpErr
|
2016-02-18 07:49:31 +02:00
|
|
|
}
|
|
|
|
}()
|
2016-04-02 23:19:39 +02:00
|
|
|
return next(c)
|
|
|
|
}
|
2021-07-15 22:34:01 +02:00
|
|
|
}, nil
|
2015-05-18 07:54:29 +02:00
|
|
|
}
|