2023-02-25 12:29:07 +03:00
---
id: error-handling
title: ๐ Error Handling
description: >-
2025-08-29 12:34:19 +02:00
Fiber supports centralized error handling: handlers return errors so you can
log them or send a custom HTTP response to the client.
2023-02-25 12:29:07 +03:00
sidebar_position: 4
---
import Tabs from '@theme/Tabs ';
import TabItem from '@theme/TabItem ';
## Catching Errors
2025-08-29 12:34:19 +02:00
Return errors from route handlers and middleware so Fiber can handle them centrally.
2023-02-25 12:29:07 +03:00
<Tabs>
<TabItem value="example" label="Example">
``` go
2024-01-08 07:31:15 -08:00
app . Get ( "/" , func ( c fiber . Ctx ) error {
2023-02-25 12:29:07 +03:00
// Pass error to Fiber
return c . SendFile ( "file-does-not-exist" )
} )
```
2024-07-11 09:21:56 -04:00
2023-02-25 12:29:07 +03:00
</TabItem>
</Tabs>
2025-08-29 12:34:19 +02:00
Fiber does not recover from [panics ](https://go.dev/blog/defer-panic-and-recover ) by default. Add the `Recover` middleware to catch panics in any handler:
2023-02-25 12:29:07 +03:00
```go title="Example"
package main
import (
"log"
2024-01-08 16:42:07 +01:00
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/recover"
2023-02-25 12:29:07 +03:00
)
func main() {
app := fiber.New()
app.Use(recover.New())
2024-01-08 07:31:15 -08:00
app.Get("/", func(c fiber.Ctx) error {
2023-02-25 12:29:07 +03:00
panic("This panic is caught by fiber")
})
log.Fatal(app.Listen(":3000"))
}
` ``
2025-08-29 12:34:19 +02:00
Use ` fiber.NewError()` to create an error with a status code. If you omit the message, Fiber uses the standard status text (for example, ` 404` becomes ` Not Found`).
2023-02-25 12:29:07 +03:00
` ``go title="Example"
2024-01-08 07:31:15 -08:00
app.Get("/", func(c fiber.Ctx) error {
2023-02-25 12:29:07 +03:00
// 503 Service Unavailable
return fiber.ErrServiceUnavailable
// 503 On vacation!
return fiber.NewError(fiber.StatusServiceUnavailable, "On vacation!")
})
` ``
## Default Error Handler
2025-08-29 12:34:19 +02:00
Fiber ships with a default error handler that sends **500 Internal Server Error** for generic errors. If the error is a [fiber.Error](https://godoc.org/github.com/gofiber/fiber#Error), the response uses the embedded status code and message.
2023-02-25 12:29:07 +03:00
` ``go title="Example"
// Default error handler
2024-01-08 07:31:15 -08:00
var DefaultErrorHandler = func(c fiber.Ctx, err error) error {
2023-02-25 12:29:07 +03:00
// Status code defaults to 500
code := fiber.StatusInternalServerError
// Retrieve the custom status code if it's a *fiber.Error
var e *fiber.Error
if errors.As(err, &e) {
code = e.Code
}
// Set Content-Type: text/plain; charset=utf-8
c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)
// Return status code with error message
return c.Status(code).SendString(err.Error())
}
` ``
## Custom Error Handler
2025-08-29 12:34:19 +02:00
Set a custom error handler in [` fiber.Config`](../api/fiber.md#errorhandler) when creating a new app.
2023-02-25 12:29:07 +03:00
2025-08-29 12:34:19 +02:00
The default handler covers most cases, but a custom handler lets you react to specific error typesโfor example, by logging to a service or sending a tailored JSON or HTML response.
2023-02-25 12:29:07 +03:00
The following example shows how to display error pages for different types of errors.
` ``go title="Example"
// Create a new fiber instance with custom config
app := fiber.New(fiber.Config{
// Override default error handler
2024-01-08 07:31:15 -08:00
ErrorHandler: func(ctx fiber.Ctx, err error) error {
2023-02-25 12:29:07 +03:00
// Status code defaults to 500
code := fiber.StatusInternalServerError
// Retrieve the custom status code if it's a *fiber.Error
var e *fiber.Error
if errors.As(err, &e) {
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(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
// Return from handler
return nil
},
})
// ...
` ``
2025-08-29 12:34:19 +02:00
> Special thanks to the [Echo](https://echo.labstack.com/) and [Express](https://expressjs.com/) frameworks for inspiring parts of this error-handling approach.