From 3e7a3fb9ad43871b6d0168ed60fa4212c7eaf93b Mon Sep 17 00:00:00 2001 From: Jo YoHan Date: Wed, 29 May 2024 12:24:47 +0900 Subject: [PATCH] feat: add Error Handler middleware --- infra/router/middleware/middleware.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/infra/router/middleware/middleware.go b/infra/router/middleware/middleware.go index 76436c1..58cacef 100644 --- a/infra/router/middleware/middleware.go +++ b/infra/router/middleware/middleware.go @@ -1,6 +1,9 @@ package middleware import ( + "log/slog" + "net/http" + "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" ) @@ -13,3 +16,13 @@ func CorsHandler() gin.HandlerFunc { AllowOrigins: []string{"*"}, }) } + +func ErrorHandler(c *gin.Context) { + c.Next() + + for _, err := range c.Errors { + slog.Error(err.Error()) + } + + c.JSON(http.StatusInternalServerError, "") +}