From 84ea5529d2e4a574940baa90e2bce6edd4a34ebe Mon Sep 17 00:00:00 2001 From: timerzz Date: Tue, 2 Jul 2024 17:50:49 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=20=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=8C=E7=BB=9F=E4=B8=80=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- subtitles/main.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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())