Merge 1c674c9e6bd066d73799caac2196018d6cc9a523 into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646

This commit is contained in:
Aleksey Fedorov 2026-11-13 00:06:24 +10:00 committed by GitHub
commit 1ed775bb45
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 2 deletions

View File

@ -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)
}
}

View File

@ -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))