mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
Co-authored-by: Becir Basic <bb@neotel.at>
This commit is contained in:
parent
05df10c62f
commit
5c38c3b770
@ -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)
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user