Recover (恢复) 中间件

Recover 中间件从 panic 链中的任意位置恢复程序, 打印堆栈的错误信息,并将错误集中交给 HTTPErrorHandler 处理。

用法

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

自定义配置

用法

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

上面的示例使用了 1 kb 的 StackSize 和默认值的 DisableStackAllDisablePrintStack

配置

  1. RecoverConfig struct {
  2. // Skipper defines a function to skip middleware.
  3. Skipper Skipper
  4. // Size of the stack to be printed.
  5. // Optional. Default value 4KB.
  6. StackSize int `json:"stack_size"`
  7. // DisableStackAll disables formatting stack traces of all other goroutines
  8. // into buffer after the trace for the current goroutine.
  9. // Optional. Default value false.
  10. DisableStackAll bool `json:"disable_stack_all"`
  11. // DisablePrintStack disables printing stack trace.
  12. // Optional. Default value as false.
  13. DisablePrintStack bool `json:"disable_print_stack"`
  14. }

默认配置

  1. DefaultRecoverConfig = RecoverConfig{
  2. Skipper: defaultSkipper,
  3. StackSize: 4 << 10, // 4 KB
  4. DisableStackAll: false,
  5. DisablePrintStack: false,
  6. }