Skip to content

Commit

Permalink
Avoid panic when used with the recover middleware (#14)
Browse files Browse the repository at this point in the history
* Avoid panic when used with the recover middleware

When using a custom log handler and the recover middleware,
err is (sometimes?) nil.

---------

Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
  • Loading branch information
abh and samber authored Oct 24, 2023
1 parent f44db81 commit 93105a7
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,18 @@ func NewWithConfig(logger *slog.Logger, config Config) echo.MiddlewareFunc {
msg := "Incoming request"
if status >= http.StatusInternalServerError {
level = config.ServerErrorLevel
msg = err.Error()
if err != nil {
msg = err.Error()
} else {
msg = http.StatusText(status)
}
} else if status >= http.StatusBadRequest && status < http.StatusInternalServerError {
level = config.ClientErrorLevel
msg = err.Error()
if err != nil {
msg = err.Error()
} else {
msg = http.StatusText(status)
}
}

logger.LogAttrs(c.Request().Context(), level, msg, attributes...)
Expand Down

0 comments on commit 93105a7

Please sign in to comment.