diff --git a/backend/internal/handler/ops_capture_writer_nil_test.go b/backend/internal/handler/ops_capture_writer_nil_test.go new file mode 100644 index 0000000000..76f50b8e45 --- /dev/null +++ b/backend/internal/handler/ops_capture_writer_nil_test.go @@ -0,0 +1,36 @@ +package handler + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestOpsCaptureWriter_NilInnerWriter_NoPanic(t *testing.T) { + w := &opsCaptureWriter{} + w.ResponseWriter = nil + + assert.NotPanics(t, func() { + assert.Equal(t, 0, w.Status()) + }, "Status() on released writer must not panic") + + assert.NotPanics(t, func() { + assert.Equal(t, -1, w.Size()) + }, "Size() on released writer must not panic") + + assert.NotPanics(t, func() { + assert.False(t, w.Written()) + }, "Written() on released writer must not panic") + + assert.NotPanics(t, func() { + n, err := w.Write([]byte("test")) + assert.Equal(t, 0, n) + assert.NoError(t, err) + }, "Write() on released writer must not panic") + + assert.NotPanics(t, func() { + n, err := w.WriteString("test") + assert.Equal(t, 0, n) + assert.NoError(t, err) + }, "WriteString() on released writer must not panic") +} diff --git a/backend/internal/handler/ops_error_logger.go b/backend/internal/handler/ops_error_logger.go index 5a1e57ff7d..16aee7fe5a 100644 --- a/backend/internal/handler/ops_error_logger.go +++ b/backend/internal/handler/ops_error_logger.go @@ -498,7 +498,31 @@ func releaseOpsCaptureWriter(w *opsCaptureWriter) { opsCaptureWriterPool.Put(w) } +func (w *opsCaptureWriter) Status() int { + if w.ResponseWriter == nil { + return 0 + } + return w.ResponseWriter.Status() +} + +func (w *opsCaptureWriter) Size() int { + if w.ResponseWriter == nil { + return -1 + } + return w.ResponseWriter.Size() +} + +func (w *opsCaptureWriter) Written() bool { + if w.ResponseWriter == nil { + return false + } + return w.ResponseWriter.Written() +} + func (w *opsCaptureWriter) Write(b []byte) (int, error) { + if w.ResponseWriter == nil { + return 0, nil + } if w.Status() >= 400 && w.limit > 0 && w.buf.Len() < w.limit { remaining := w.limit - w.buf.Len() if len(b) > remaining { @@ -511,6 +535,9 @@ func (w *opsCaptureWriter) Write(b []byte) (int, error) { } func (w *opsCaptureWriter) WriteString(s string) (int, error) { + if w.ResponseWriter == nil { + return 0, nil + } if w.Status() >= 400 && w.limit > 0 && w.buf.Len() < w.limit { remaining := w.limit - w.buf.Len() if len(s) > remaining {