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:
parent
70acd57105
commit
a9879ffa6b
@ -2,9 +2,10 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Example for `fmt.Printf`
|
// Example for `fmt.Printf`
|
||||||
@ -264,7 +265,8 @@ func (config RequestLoggerConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
|
|||||||
if config.LogStatus {
|
if config.LogStatus {
|
||||||
v.Status = res.Status
|
v.Status = res.Status
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if httpErr, ok := err.(*echo.HTTPError); ok {
|
var httpErr *echo.HTTPError
|
||||||
|
if errors.As(err, &httpErr) {
|
||||||
v.Status = httpErr.Code
|
v.Status = httpErr.Code
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -196,8 +197,8 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
he, ok := err.(*echo.HTTPError)
|
var he *echo.HTTPError
|
||||||
if !(ok && config.HTML5 && he.Code == http.StatusNotFound) {
|
if !(errors.As(err, &he) && config.HTML5 && he.Code == http.StatusNotFound) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user