description: >- Fiber supports centralized error handling by returning an error to the handler which allows you to log errors to external services or send a customized HTTP
response to the client.
🐛 Error Handling
Catching Errors
It’s essential to ensure that Fiber catches all errors that occur while running route handlers and middleware. You must return them to the handler function, where Fiber will catch and process them.
app.Get("/", func(c *fiber.Ctx) error {
// Pass error to Fiber
return c.SendFile("file-does-not-exist")
})
Fiber does not handle panics by default. To recover from a panic thrown by any handler in the stack, you need to include the Recover
middleware below:
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/recover"
)
func main() {
app := fiber.New()
app.Use(recover.New())
app.Get("/", func(c *fiber.Ctx) error {
panic("This panic is caught by fiber")
})
log.Fatal(app.Listen(":3000"))
}
You could use Fiber’s custom error struct to pass an additional status code
using fiber.NewError()
. It’s optional to pass a message; if this is left empty, it will default to the status code message (404
equals Not Found
).
app.Get("/", func(c *fiber.Ctx) error {
// 503 Service Unavailable
return fiber.ErrServiceUnavailable
// 503 On vacation!
return fiber.NewError(fiber.StatusServiceUnavailable, "On vacation!")
})
Default Error Handler
Fiber provides an error handler by default. For a standard error, the response is sent as 500 Internal Server Error. If the error is of type fiber.Error, the response is sent with the provided status code and message.
// Default error handler
var DefaultErrorHandler = func(c *fiber.Ctx, err error) error {
// Default 500 statuscode
code := fiber.StatusInternalServerError
if e, ok := err.(*fiber.Error); ok {
// Override status code if fiber.Error type
code = e.Code
}
// Set Content-Type: text/plain; charset=utf-8
c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)
// Return statuscode with error message
return c.Status(code).SendString(err.Error())
}
Custom Error Handler
A custom error handler can be set using a Config when initializing a Fiber instance.
In most cases, the default error handler should be sufficient. However, a custom error handler can come in handy if you want to capture different types of errors and take action accordingly e.g., send a notification email or log an error to the centralized system. You can also send customized responses to the client e.g., error page or just a JSON response.
The following example shows how to display error pages for different types of errors.
// Create a new fiber instance with custom config
app := fiber.New(fiber.Config{
// Override default error handler
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
// Status code defaults to 500
code := fiber.StatusInternalServerError
// Retrieve the custom status code if it's an fiber.*Error
if e, ok := err.(*fiber.Error); ok {
code = e.Code
}
// Send custom error page
err = ctx.Status(code).SendFile(fmt.Sprintf("./%d.html", code))
if err != nil {
// In case the SendFile fails
return ctx.Status(500).SendString("Internal Server Error")
}
// Return from handler
return nil
},
})
// ...
Special thanks to the Echo & Express framework for inspiration regarding error handling.