From e7a1590845f84db03483c79d084a9c29f483af6b Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Sun, 12 May 2024 00:38:43 +0200 Subject: [PATCH] feat: add internal error --- example/example.go | 5 ++++- middleware.go | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/example/example.go b/example/example.go index 5bb2f96..90b63cd 100644 --- a/example/example.go +++ b/example/example.go @@ -1,6 +1,7 @@ package main import ( + "errors" "net/http" "os" "time" @@ -47,7 +48,9 @@ func main() { return c.String(http.StatusOK, "Hello, World!") }) e.GET("/error", func(c echo.Context) error { - return echo.NewHTTPError(http.StatusInternalServerError, "A simulated error") + return echo. + NewHTTPError(http.StatusInternalServerError, "A simulated error"). + WithInternal(errors.New("A simulated internal error")) }) // Start server diff --git a/middleware.go b/middleware.go index b2eb9da..f81c54f 100644 --- a/middleware.go +++ b/middleware.go @@ -264,6 +264,7 @@ func NewWithConfig(logger *slog.Logger, config Config) echo.MiddlewareFunc { level := config.DefaultLevel msg := "Incoming request" + if status >= http.StatusInternalServerError { level = config.ServerErrorLevel if err != nil { @@ -280,6 +281,18 @@ func NewWithConfig(logger *slog.Logger, config Config) echo.MiddlewareFunc { } } + if httpErr != nil { + attributes = append( + attributes, + slog.Any("error", map[string]any{ + "code": httpErr.Code, + "message": httpErr.Message, + "internal": httpErr.Internal, + }), + slog.String("internal", httpErr.Internal.Error()), + ) + } + logger.LogAttrs(c.Request().Context(), level, msg, attributes...) return