Skip to content

Commit

Permalink
feat: add internal error
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed May 11, 2024
1 parent 854bfce commit e7a1590
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion example/example.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"net/http"
"os"
"time"
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit e7a1590

Please sign in to comment.