Skip to content

Commit

Permalink
Plugs the Error Handler into the Gin adaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Dec 20, 2024
1 parent 6ecde1f commit 9ad81a5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions examples/gin-compat/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 13 additions & 3 deletions extra/fuegogin/adaptor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fuegogin

import (
"errors"
"log/slog"
"net/http"

Expand Down Expand Up @@ -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] {
Expand All @@ -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]{
Expand All @@ -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
}

Expand All @@ -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
}
2 changes: 1 addition & 1 deletion extra/fuegogin/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 9ad81a5

Please sign in to comment.