diff --git a/subtitles/main.go b/subtitles/main.go index f7dd31f..ac68e4f 100644 --- a/subtitles/main.go +++ b/subtitles/main.go @@ -1,6 +1,7 @@ package main import ( + "errors" "github.com/go-playground/validator/v10" "github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3/middleware/cors" @@ -23,6 +24,26 @@ func main() { s := &Service{cli: cli} app := fiber.New(fiber.Config{ StructValidator: &structValidator{validate: validator.New()}, + ErrorHandler: func(ctx fiber.Ctx, err error) error { + // 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).JSON(fiber.Map{ + "data": "", + "code": code, + "msg": err.Error(), + }) + + // Return from handler + return nil + }, }) app.Use(recover.New(), cors.New())