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
- go get -u github.com/gofiber/recover
Signature
- recover.New(config ...Config) func(*Ctx)
Example
- package main
- import (
- "github.com/gofiber/fiber"
- "github.com/gofiber/recover"
- )
- func main() {
- app := fiber.New()
- // Optional
- cfg := recover.Config{
- Handler: func(c *fiber.Ctx, err error) {
- c.SendString(err.Error())
- c.SendStatus(500)
- },
- }
- app.Use(recover.New(cfg))
- app.Get("/", func(c *fiber.Ctx) {
- panic("Hi, I'm a error!")
- })
- app.Listen(3000)
- }