Recover

You can recover from panic errors within any route. By default the Recover middleware will respond with 500 Internal Server Error when a panic occurs. You can also provide your own error handler.

Installation

  1. go get -u github.com/gofiber/recover

Signature

  1. recover.New(config ...Config) func(*Ctx)

Example

  1. package main
  2.  
  3. import (
  4. "github.com/gofiber/fiber"
  5. "github.com/gofiber/recover"
  6. )
  7.  
  8. func main() {
  9. app := fiber.New()
  10.  
  11. // Optional
  12. cfg := recover.Config{
  13. Handler: func(c *fiber.Ctx, err error) {
  14. c.SendString(err.Error())
  15. c.SendStatus(500)
  16. },
  17. }
  18.  
  19. app.Use(recover.New(cfg))
  20.  
  21. app.Get("/", func(c *fiber.Ctx) {
  22. panic("Hi, I'm a error!")
  23. })
  24.  
  25. app.Listen(3000)
  26. }