diff --git a/middleware/body_dump.go b/middleware/body_dump.go index e4119ec1e..da558e883 100644 --- a/middleware/body_dump.go +++ b/middleware/body_dump.go @@ -6,6 +6,7 @@ package middleware import ( "bufio" "bytes" + "fmt" "errors" "io" "net" @@ -67,7 +68,11 @@ func BodyDumpWithConfig(config BodyDumpConfig) echo.MiddlewareFunc { // Request reqBody := []byte{} if c.Request().Body != nil { // Read - reqBody, _ = io.ReadAll(c.Request().Body) + var errRead error + reqBody, errRead = io.ReadAll(c.Request().Body) + if errRead != nil { + return errRead + } } c.Request().Body = io.NopCloser(bytes.NewBuffer(reqBody)) // Reset @@ -100,7 +105,7 @@ func (w *bodyDumpResponseWriter) Write(b []byte) (int, error) { func (w *bodyDumpResponseWriter) Flush() { err := http.NewResponseController(w.ResponseWriter).Flush() if err != nil && errors.Is(err, http.ErrNotSupported) { - panic(errors.New("response writer flushing is not supported")) + panic(fmt.Errorf("echo: response writer %T does not support flushing (http.Flusher interface)", w.ResponseWriter)) } } diff --git a/middleware/body_dump_test.go b/middleware/body_dump_test.go index e880af45b..6abebade1 100644 --- a/middleware/body_dump_test.go +++ b/middleware/body_dump_test.go @@ -5,6 +5,7 @@ package middleware import ( "errors" + "fmt" "io" "net/http" "net/http/httptest" @@ -92,11 +93,12 @@ func TestBodyDumpFails(t *testing.T) { } func TestBodyDumpResponseWriter_CanNotFlush(t *testing.T) { + rw := new(testResponseWriterNoFlushHijack) bdrw := bodyDumpResponseWriter{ - ResponseWriter: new(testResponseWriterNoFlushHijack), // this RW does not support flush + ResponseWriter: rw, // this RW does not support flush } - assert.PanicsWithError(t, "response writer flushing is not supported", func() { + assert.PanicsWithError(t, fmt.Sprintf("echo: response writer %T does not support flushing (http.Flusher interface)", rw), func() { bdrw.Flush() }) }