From 1c674c9e6bd066d73799caac2196018d6cc9a523 Mon Sep 17 00:00:00 2001 From: Dv2Corn <320380254+Dv2Corn@users.noreply.github.com> Date: Thu, 12 Nov 2026 13:53:37 +0000 Subject: [PATCH] fix(recovery): log panic values that are not strings or errors --- recovery.go | 4 ++-- recovery_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/recovery.go b/recovery.go index 2124fe5f..146078d3 100644 --- a/recovery.go +++ b/recovery.go @@ -71,10 +71,10 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc { if isBrokenPipe { logger.Printf("%s\n%s%s", rec, secureRequestDump(c.Request), reset) } else if IsDebugging() { - logger.Printf("[Recovery] %s panic recovered:\n%s\n%s\n%s%s", + logger.Printf("[Recovery] %s panic recovered:\n%s\n%v\n%s%s", timeFormat(time.Now()), secureRequestDump(c.Request), rec, stack(stackSkip), reset) } else { - logger.Printf("[Recovery] %s panic recovered:\n%s\n%s%s", + logger.Printf("[Recovery] %s panic recovered:\n%v\n%s%s", timeFormat(time.Now()), rec, stack(stackSkip), reset) } } diff --git a/recovery_test.go b/recovery_test.go index e5211790..79aa9d78 100644 --- a/recovery_test.go +++ b/recovery_test.go @@ -76,6 +76,39 @@ func TestPanicInHandler(t *testing.T) { } // TestPanicWithAbort assert that panic has been recovered even if context.Abort was used. +// TestPanicWithNonStringValue asserts that a panic value which is neither a +// string nor an error is logged the way the runtime prints it. +func TestPanicWithNonStringValue(t *testing.T) { + type code int + + for _, mode := range []string{TestMode, DebugMode} { + SetMode(mode) + buffer := new(strings.Builder) + router := New() + router.Use(RecoveryWithWriter(buffer)) + router.GET("/int", func(_ *Context) { + panic(42) + }) + router.GET("/typed", func(_ *Context) { + panic(code(7)) + }) + // RUN + w := PerformRequest(router, http.MethodGet, "/int") + // TEST + assert.Equal(t, http.StatusInternalServerError, w.Code) + assert.Contains(t, buffer.String(), "\n42\n") + + // RUN + w = PerformRequest(router, http.MethodGet, "/typed") + // TEST + assert.Equal(t, http.StatusInternalServerError, w.Code) + assert.Contains(t, buffer.String(), "\n7\n") + assert.NotContains(t, buffer.String(), "%!s") + } + + SetMode(TestMode) +} + func TestPanicWithAbort(t *testing.T) { router := New() router.Use(RecoveryWithWriter(nil))