From 9ad81a51f2ee83810a79541142c32f208958e698 Mon Sep 17 00:00:00 2001 From: EwenQuim Date: Fri, 20 Dec 2024 19:39:54 +0100 Subject: [PATCH] Plugs the Error Handler into the Gin adaptor --- examples/gin-compat/handlers.go | 4 ++++ extra/fuegogin/adaptor.go | 16 +++++++++++++--- extra/fuegogin/context.go | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/examples/gin-compat/handlers.go b/examples/gin-compat/handlers.go index a6cbfd19..82853233 100644 --- a/examples/gin-compat/handlers.go +++ b/examples/gin-compat/handlers.go @@ -25,6 +25,10 @@ func fuegoControllerPost(c fuego.ContextWithBody[HelloRequest]) (HelloResponse, return HelloResponse{}, err } + if body.Word == "forbidden" { + return HelloResponse{}, fuego.BadRequestError{Title: "Forbidden word"} + } + ctx := c.Context().(*gin.Context) fmt.Printf("%#v", ctx) diff --git a/extra/fuegogin/adaptor.go b/extra/fuegogin/adaptor.go index d8b5806f..c2fb6cce 100644 --- a/extra/fuegogin/adaptor.go +++ b/extra/fuegogin/adaptor.go @@ -1,6 +1,7 @@ package fuegogin import ( + "errors" "log/slog" "net/http" @@ -28,7 +29,7 @@ func Post[T, B any](engine *fuego.Engine, ginRouter gin.IRouter, path string, ha func handleFuego[T, B any](engine *fuego.Engine, ginRouter gin.IRouter, method, path string, fuegoHandler func(c fuego.ContextWithBody[B]) (T, error), options ...func(*fuego.BaseRoute)) *fuego.Route[T, B] { baseRoute := fuego.NewBaseRoute(method, path, fuegoHandler, engine.OpenAPI, options...) - return handle(engine, ginRouter, &fuego.Route[T, B]{BaseRoute: baseRoute}, GinHandler(fuegoHandler)) + return handle(engine, ginRouter, &fuego.Route[T, B]{BaseRoute: baseRoute}, GinHandler(engine, fuegoHandler)) } func handleGin(engine *fuego.Engine, ginRouter gin.IRouter, method, path string, ginHandler gin.HandlerFunc, options ...func(*fuego.BaseRoute)) *fuego.Route[any, any] { @@ -52,7 +53,7 @@ func handle[T, B any](engine *fuego.Engine, ginRouter gin.IRouter, route *fuego. } // Convert a Fuego handler to a Gin handler. -func GinHandler[B, T any](handler func(c fuego.ContextWithBody[B]) (T, error)) gin.HandlerFunc { +func GinHandler[B, T any](engine *fuego.Engine, handler func(c fuego.ContextWithBody[B]) (T, error)) gin.HandlerFunc { return func(c *gin.Context) { context := &ginContext[B]{ CommonContext: internal.CommonContext[B]{ @@ -65,7 +66,8 @@ func GinHandler[B, T any](handler func(c fuego.ContextWithBody[B]) (T, error)) g resp, err := handler(context) if err != nil { - c.Error(err) + err = engine.ErrorHandler(err) + c.JSON(getErrorCode(err), err) return } @@ -77,3 +79,11 @@ func GinHandler[B, T any](handler func(c fuego.ContextWithBody[B]) (T, error)) g c.JSON(200, resp) } } + +func getErrorCode(err error) int { + var status fuego.ErrorWithStatus + if errors.As(err, &status) { + return status.StatusCode() + } + return 500 +} diff --git a/extra/fuegogin/context.go b/extra/fuegogin/context.go index 40a2ee3d..ed4a59e5 100644 --- a/extra/fuegogin/context.go +++ b/extra/fuegogin/context.go @@ -19,7 +19,7 @@ var _ fuego.ContextWithBody[any] = &ginContext[any]{} func (c ginContext[B]) Body() (B, error) { var body B - err := c.ginCtx.Bind(&body) + err := c.ginCtx.BindJSON(&body) return body, err }