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...)