Skip to content

Commit

Permalink
feat: handle panic error
Browse files Browse the repository at this point in the history
  • Loading branch information
KagChi authored Sep 21, 2024
1 parent 0ca43bc commit 6264d86
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/davidbyttow/govips/v2/vips"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/joho/godotenv"
"github.com/nezuchan/image-proxy/impl/image_resizer/client"
Expand All @@ -24,7 +25,27 @@ func main() {
defer vips.Shutdown()

_ = godotenv.Load()
app := fiber.New()
app := fiber.New(fiber.Config{
Prefork: true,
ServerHeader: "Fiber",
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) {

Check failure on line 37 in cmd/server/main.go

View workflow job for this annotation

GitHub Actions / test / compile

undefined: errors
code = e.Code
}

return ctx.Status(code).JSON(&struct_type.ErrorResponse{

Check failure on line 41 in cmd/server/main.go

View workflow job for this annotation

GitHub Actions / test / compile

undefined: struct_type
Message: "Something went wrong, please try again later.",
Error: err.Error(),
})
},
})

app.Use(recover.New())

app.Use(logger.New(logger.Config{
Format: "[${ip}]:${port} [${latency}] ${status} - ${method} ${path}\n",
Expand Down

0 comments on commit 6264d86

Please sign in to comment.