Fixed: errors catching in Fiber

This commit is contained in:
Aleksandr Soloshenko 2023-11-01 22:55:24 +07:00
parent 3431c4e2fc
commit 6ecd8ea127
3 changed files with 16 additions and 1 deletions

View File

@ -16,3 +16,17 @@ func AsApiHandler(f any) any {
fx.ResultTags(`group:"api-routes"`),
)
}
func errorHandler(c *fiber.Ctx, err error) error {
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 json error
return c.Status(code).JSON(&fiber.Map{
"message": err.Error(),
})
}

View File

@ -23,6 +23,7 @@ func New(params Params) (*fiber.App, error) {
WriteTimeout: WriteTimeout,
IdleTimeout: IdleTimeout,
DisableStartupMessage: true,
ErrorHandler: errorHandler,
})
app.Use(recover.New())

View File

@ -9,7 +9,7 @@ import (
func New() fiber.Handler {
return func(c *fiber.Ctx) error {
if err := c.Next(); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": err.Error()})
return err
}
contentType := string(c.Response().Header.ContentType())