Skip to content

Commit a9879ff

Browse files
danielbpricealdas
authored andcommitted
Middlewares should use errors.As() instead of type assertion on HTTPError
- Helps consumers who want to wrap HTTPError, and other use cases
1 parent 70acd57 commit a9879ff

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

middleware/request_logger.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package middleware
22

33
import (
44
"errors"
5-
"github.com/labstack/echo/v4"
65
"net/http"
76
"time"
7+
8+
"github.com/labstack/echo/v4"
89
)
910

1011
// Example for `fmt.Printf`
@@ -264,7 +265,8 @@ func (config RequestLoggerConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
264265
if config.LogStatus {
265266
v.Status = res.Status
266267
if err != nil {
267-
if httpErr, ok := err.(*echo.HTTPError); ok {
268+
var httpErr *echo.HTTPError
269+
if errors.As(err, &httpErr) {
268270
v.Status = httpErr.Code
269271
}
270272
}

middleware/static.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package middleware
22

33
import (
4+
"errors"
45
"fmt"
56
"html/template"
67
"net/http"
@@ -196,8 +197,8 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
196197
return err
197198
}
198199

199-
he, ok := err.(*echo.HTTPError)
200-
if !(ok && config.HTML5 && he.Code == http.StatusNotFound) {
200+
var he *echo.HTTPError
201+
if !(errors.As(err, &he) && config.HTML5 && he.Code == http.StatusNotFound) {
201202
return err
202203
}
203204

0 commit comments

Comments
 (0)