Skip to content

Commit

Permalink
Preserve echo.HTTPError error types passing through middleware (#39)
Browse files Browse the repository at this point in the history
Without this patch all errors will be downgraded to a basic error type
when going through this middleware
  • Loading branch information
abh authored Jul 22, 2024
1 parent 20f72b9 commit 85dd438
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,13 @@ func NewWithConfig(logger *slog.Logger, config Config) echo.MiddlewareFunc {
ip := c.RealIP()
referer := c.Request().Referer()

errMsg := err

var httpErr *echo.HTTPError
if err != nil && errors.As(err, &httpErr) {
status = httpErr.Code
if msg, ok := httpErr.Message.(string); ok {
err = errors.New(msg)
errMsg = errors.New(msg)
}
}

Expand Down Expand Up @@ -271,14 +273,14 @@ func NewWithConfig(logger *slog.Logger, config Config) echo.MiddlewareFunc {
if status >= http.StatusInternalServerError {
level = config.ServerErrorLevel
if err != nil {
msg = err.Error()
msg = errMsg.Error()
} else {
msg = http.StatusText(status)
}
} else if status >= http.StatusBadRequest && status < http.StatusInternalServerError {
level = config.ClientErrorLevel
if err != nil {
msg = err.Error()
msg = errMsg.Error()
} else {
msg = http.StatusText(status)
}
Expand Down

0 comments on commit 85dd438

Please sign in to comment.