Recover

Recover middleware for Fiber that recovers from panics anywhere in the stack chain and handles the control to the centralized ErrorHandler.

Signatures

  1. func New(config ...Config) fiber.Handler

Examples

Import the middleware package that is part of the Fiber web framework

  1. import (
  2. "github.com/gofiber/fiber/v2"
  3. "github.com/gofiber/fiber/v2/middleware/recover"
  4. )

After you initiate your Fiber app, you can use the following possibilities:

  1. // Default middleware config
  2. app.Use(recover.New())
  3. // This panic will be catch by the middleware
  4. app.Get("/", func(c *fiber.Ctx) error {
  5. panic("I'm an error")
  6. })

Config

  1. // Config defines the config for middleware.
  2. type Config struct {
  3. // Next defines a function to skip this middleware when returned true.
  4. //
  5. // Optional. Default: nil
  6. Next func(c *fiber.Ctx) bool
  7. // EnableStackTrace enables handling stack trace
  8. //
  9. // Optional. Default: false
  10. EnableStackTrace bool
  11. // StackTraceHandler defines a function to handle stack trace
  12. //
  13. // Optional. Default: defaultStackTraceHandler
  14. StackTraceHandler func(e interface{})
  15. }

Default Config

  1. var ConfigDefault = Config{
  2. Next: nil,
  3. EnableStackTrace: false,
  4. StackTraceHandler: defaultStackTraceHandler,
  5. }