From 4c52b9749bd98449eb56212c1f53f822675ec9da Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Mon, 13 May 2024 14:50:36 +0200 Subject: [PATCH] fix segfault --- example/example.go | 8 +++++++- middleware.go | 5 ++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/example/example.go b/example/example.go index 90b63cd..8156bae 100644 --- a/example/example.go +++ b/example/example.go @@ -47,11 +47,17 @@ func main() { slogecho.AddCustomAttributes(c, slog.String("foo", "bar")) return c.String(http.StatusOK, "Hello, World!") }) - e.GET("/error", func(c echo.Context) error { + e.GET("/error1", func(c echo.Context) error { return echo. NewHTTPError(http.StatusInternalServerError, "A simulated error"). WithInternal(errors.New("A simulated internal error")) }) + e.GET("/error2", func(c echo.Context) error { + return echo.NewHTTPError(http.StatusInternalServerError, "A simulated error") + }) + e.GET("/error3", func(c echo.Context) error { + return errors.New("A simulated error") + }) // Start server e.Logger.Fatal(e.Start(":4242")) diff --git a/middleware.go b/middleware.go index f81c54f..1534357 100644 --- a/middleware.go +++ b/middleware.go @@ -289,8 +289,11 @@ func NewWithConfig(logger *slog.Logger, config Config) echo.MiddlewareFunc { "message": httpErr.Message, "internal": httpErr.Internal, }), - slog.String("internal", httpErr.Internal.Error()), ) + + if httpErr.Internal != nil { + attributes = append(attributes, slog.String("internal", httpErr.Internal.Error())) + } } logger.LogAttrs(c.Request().Context(), level, msg, attributes...)