Skip to content

Commit

Permalink
Add custom err handler
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiante committed Oct 20, 2024
1 parent c59287e commit d78018c
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions api/err.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,16 @@ func respondWithError(ctx *gin.Context, status int, err error) {

ctx.AbortWithStatusJSON(status, response)
}

type ErrHandler func(c *gin.Context) error

func AsErrHandler(h ErrHandler) gin.HandlerFunc {
return func(ctx *gin.Context) {
err := h(ctx)

if err != nil {
_ = ctx.AbortWithError(500, err)
return
}
}
}

0 comments on commit d78018c

Please sign in to comment.