8000 [HttpKernel] Fix logging of post-terminate errors/exceptions · symfony/symfony@e85b371 · GitHub
[go: up one dir, main page]

Skip to content

Commit e85b371

Browse files
[HttpKernel] Fix logging of post-terminate errors/exceptions
1 parent 40ced3a commit e85b371

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class DebugHandlersListener implements EventSubscriberInterface
3636
private $scream;
3737
private $fileLinkFormat;
3838
private $firstCall = true;
39+
private $hasTerminatedWithException;
3940

4041
/**
4142
* @param callable|null $exceptionHandler A handler that will be called on Exception
@@ -60,14 +61,16 @@ public function __construct($exceptionHandler, LoggerInterface $logger = null, $
6061
*/
6162
public function configure(Event $event = null)
6263
{
63-
if (!$this->firstCall) {
64+
if (!$event instanceof KernelEvent ? !$this->firstCall : !$event->isMasterRequest()) {
6465
return;
6566
}
66-
$this->firstCall = false;
67+
$this->firstCall = $this->hasTerminatedWithException = false;
68+
69+
$handler = set_exception_handler('var_dump');
70+
$handler = is_array($handler) ? $handler[0] : null;
71+
restore_exception_handler();
72+
6773
if ($this->logger || null !== $this->throwAt) {
68-
$handler = set_error_handler('var_dump');
69-
$handler = is_array($handler) ? $handler[0] : null;
70-
restore_error_handler();
7174
if ($handler instanceof ErrorHandler) {
7275
if ($this->logger) {
7376
$handler->setDefaultLogger($this->logger, $this->levels);
@@ -91,8 +94,16 @@ public function configure(Event $event = null)
9194
}
9295
if (!$this->exceptionHandler) {
9396
if ($event instanceof KernelEvent) {
94-
if (method_exists($event->getKernel(), 'terminateWithException')) {
95-
$this->exceptionHandler = array($event->getKernel(), 'terminateWithException');
97+
if (method_exists($kernel = $event->getKernel(), 'terminateWithException')) {
98+
$request = $event->getRequest();
99+
$hasRun = &$this->hasTerminatedWithException;
100+
$this->exceptionHandler = function (\Exception $e) use ($kernel, $request, &$hasRun) {
101+
if ($hasRun) {
102+
throw $e;
103+
}
104+
$hasRun = true;
105+
$kernel->terminateWithException($e, $request);
106+
};
96107
}
97108
} elseif ($event instanceof ConsoleEvent && $app = $event->getCommand()->getApplication()) {
98109
$output = $event->getOutput();
@@ -105,9 +116,6 @@ public function configure(Event $event = null)
105116
}
106117
}
107118
if ($this->exceptionHandler) {
108-
$handler = set_exception_handler('var_dump');
109-
$handler = is_array($handler) ? $handler[0] : null;
110-
restore_exception_handler();
111119
if ($handler instanceof ErrorHandler) {
112120
$h = $handler->setExceptionHandler('var_dump') ?: $this->exceptionHandler;
113121
$handler->setExceptionHandler($h);

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,12 @@ public function terminate(Request $request, Response $response)
7878
}
7979

8080
/**
81-
* @throws \LogicException If the request stack is empty
82-
*
8381
* @internal
8482
*/
85-
public function terminateWithException(\Exception $exception)
83+
public function terminateWithException(\Exception $exception, Request $request = null)
8684
{
87-
if (!$request = $this->requestStack->getMasterRequest()) {
88-
throw new \LogicException('Request stack is empty', 0, $exception);
85+
if (!$request = $request ?: $this->requestStack->getMasterRequest()) {
86+
throw $exception;
8987
}
9088

9189
$response = $this->handleException($exception, $request, self::MASTER_REQUEST);

0 commit comments

Comments
 (0)
0