1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-12 01:22:21 +02:00

Middlewares should use errors.As() instead of type assertion on HTTPError

- Helps consumers who want to wrap HTTPError, and other use cases
This commit is contained in:
Daniel Price 2022-07-21 17:40:44 +00:00 committed by Martti T
parent 70acd57105
commit a9879ffa6b
2 changed files with 7 additions and 4 deletions

View File

@ -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`
@ -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
}
}

View File

@ -1,6 +1,7 @@
package middleware
import (
"errors"
"fmt"
"html/template"
"net/http"
@ -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
}