Skip to content

Commit ad3f44e

Browse files
bug symfony#58289 [HttpKernel] Skip logging uncaught exceptions in ErrorHandler, assume $kernel->terminateWithException() will do it (nicolas-grekas)
This PR was merged into the 5.4 branch. Discussion ---------- [HttpKernel] Skip logging uncaught exceptions in `ErrorHandler`, assume `$kernel->terminateWithException()` will do it | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#57902 | License | MIT When uncaught exceptions are handled by `ErrorHandler::handleException()`, that method logs them, then calls a callback to let the app know about the exception itself. This causes two issues: 1. that log happens without context, which leads to symfony#57902 2. logs are duplicated: one by ErrorHandler, twice by the callback In this PR, I propose to consider that if terminateWithException is configured as a callback, we assume that the app will handle the logging on its own. Tested in practice, this works nicely. Adding a test case is quite complex as all this involves many pieces and global state, so I skipped adding one. Commits ------- 3b87c32 [HttpKernel] Skip logging uncaught exceptions in ErrorHandler, assume $kernel->terminateWithException() will do it
2 parents 56fa4dc + 3b87c32 commit ad3f44e

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/Symfony/Component/HttpKernel/EventListener/DebugHandlersListener.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function configure(?object $event = null)
8282
return;
8383
}
8484
$this->firstCall = $this->hasTerminatedWithException = false;
85+
$hasRun = null;
8586

8687
$handler = set_exception_handler('is_int');
8788
$handler = \is_array($handler) ? $handler[0] : null;
@@ -144,6 +145,19 @@ public function configure(?object $event = null)
144145
if ($this->exceptionHandler) {
145146
if ($handler instanceof ErrorHandler) {
146147
$handler->setExceptionHandler($this->exceptionHandler);
148+
if (null !== $hasRun) {
149+
$throwAt = $handler->throwAt(0) | \E_ERROR | \E_CORE_ERROR | \E_COMPILE_ERROR | \E_USER_ERROR | \E_RECOVERABLE_ERROR | \E_PARSE;
150+
$loggers = [];
151+
152+
foreach ($handler->setLoggers([]) as $type => $log) {
153+
if ($type & $throwAt) {
154+
$loggers[$type] = [null, $log[1]];
155+
}
156+
}
157+
158+
// Assume $kernel->terminateWithException() will log uncaught exceptions appropriately
159+
$handler->setLoggers($loggers);
160+
}
147161
}
148162
$this->exceptionHandler = null;
149163
}

0 commit comments

Comments
 (0)