Recover

Recover middleware recovers from panics anywhere in the chain, prints stack trace and handles the control to the centralized HTTPErrorHandler.

Usage

  1. e.Use(middleware.Recover())

Custom Configuration

Usage

  1. e := echo.New()
  2. e.Use(middleware.RecoverWithConfig(middleware.RecoverConfig{
  3. StackSize: 1 << 10, // 1 KB
  4. LogLevel: log.ERROR,
  5. }))

Example above uses a StackSize of 1 KB, LogLevel of error and default values for DisableStackAll and DisablePrintStack.

Configuration

  1. // LogErrorFunc defines a function for custom logging in the middleware.
  2. LogErrorFunc func(c echo.Context, err error, stack []byte) error
  3. RecoverConfig struct {
  4. // Skipper defines a function to skip middleware.
  5. Skipper Skipper
  6. // Size of the stack to be printed.
  7. // Optional. Default value 4KB.
  8. StackSize int `yaml:"stack_size"`
  9. // DisableStackAll disables formatting stack traces of all other goroutines
  10. // into buffer after the trace for the current goroutine.
  11. // Optional. Default value false.
  12. DisableStackAll bool `yaml:"disable_stack_all"`
  13. // DisablePrintStack disables printing stack trace.
  14. // Optional. Default value as false.
  15. DisablePrintStack bool `yaml:"disable_print_stack"`
  16. // LogLevel is log level to printing stack trace.
  17. // Optional. Default value 0 (Print).
  18. LogLevel log.Lvl
  19. // LogErrorFunc defines a function for custom logging in the middleware.
  20. // If it's set you don't need to provide LogLevel for config.
  21. LogErrorFunc LogErrorFunc
  22. }

Default Configuration

  1. DefaultRecoverConfig = RecoverConfig{
  2. Skipper: DefaultSkipper,
  3. StackSize: 4 << 10, // 4 KB
  4. DisableStackAll: false,
  5. DisablePrintStack: false,
  6. LogLevel: 0,
  7. LogErrorFunc: nil,
  8. }