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 的 StackSizeDisableStackAllDisablePrintStack 使用默认值。

配置

  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. }