Skip to content

Commit

Permalink
Middlewares should use errors.As() instead of type assertion on HTTPE…
Browse files Browse the repository at this point in the history
…rror

- Helps consumers who want to wrap HTTPError, and other use cases
  • Loading branch information
danielbprice authored and aldas committed Jul 21, 2022
1 parent 70acd57 commit a9879ff
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 4 additions & 2 deletions middleware/request_logger.go
Expand Up @@ -2,9 +2,10 @@ package middleware

import (
"errors"
"github.com/labstack/echo/v4"
"net/http"
"time"

"github.com/labstack/echo/v4"
)

// Example for `fmt.Printf`
Expand Down Expand Up @@ -264,7 +265,8 @@ func (config RequestLoggerConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
if config.LogStatus {
v.Status = res.Status
if err != nil {
if httpErr, ok := err.(*echo.HTTPError); ok {
var httpErr *echo.HTTPError
if errors.As(err, &httpErr) {
v.Status = httpErr.Code
}
}
Expand Down
5 changes: 3 additions & 2 deletions middleware/static.go
@@ -1,6 +1,7 @@
package middleware

import (
"errors"
"fmt"
"html/template"
"net/http"
Expand Down Expand Up @@ -196,8 +197,8 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
return err
}

he, ok := err.(*echo.HTTPError)
if !(ok && config.HTML5 && he.Code == http.StatusNotFound) {
var he *echo.HTTPError
if !(errors.As(err, &he) && config.HTML5 && he.Code == http.StatusNotFound) {
return err
}

Expand Down

0 comments on commit a9879ff

Please sign in to comment.