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

Recover middleware should not log panic for aborted handler (#2134, fixes #2133)

Co-authored-by: Becir Basic <bb@neotel.at>
This commit is contained in:
Becir Basic 2022-03-16 00:29:42 +01:00 committed by GitHub
parent 05df10c62f
commit 5c38c3b770
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package middleware
import ( import (
"fmt" "fmt"
"net/http"
"runtime" "runtime"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@ -77,6 +78,9 @@ func RecoverWithConfig(config RecoverConfig) echo.MiddlewareFunc {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
if r == http.ErrAbortHandler {
panic(r)
}
err, ok := r.(error) err, ok := r.(error)
if !ok { if !ok {
err = fmt.Errorf("%v", r) err = fmt.Errorf("%v", r)

View File

@ -28,6 +28,35 @@ func TestRecover(t *testing.T) {
assert.Contains(t, buf.String(), "PANIC RECOVER") assert.Contains(t, buf.String(), "PANIC RECOVER")
} }
func TestRecoverErrAbortHandler(t *testing.T) {
e := echo.New()
buf := new(bytes.Buffer)
e.Logger.SetOutput(buf)
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
h := Recover()(echo.HandlerFunc(func(c echo.Context) error {
panic(http.ErrAbortHandler)
}))
defer func() {
r := recover()
if r == nil {
assert.Fail(t, "expecting `http.ErrAbortHandler`, got `nil`")
} else {
if err, ok := r.(error); ok {
assert.ErrorIs(t, err, http.ErrAbortHandler)
} else {
assert.Fail(t, "not of error type")
}
}
}()
h(c)
assert.Equal(t, http.StatusInternalServerError, rec.Code)
assert.NotContains(t, buf.String(), "PANIC RECOVER")
}
func TestRecoverWithConfig_LogLevel(t *testing.T) { func TestRecoverWithConfig_LogLevel(t *testing.T) {
tests := []struct { tests := []struct {
logLevel log.Lvl logLevel log.Lvl