8000 [HttpKernel] Fix logging of post-terminate errors/exceptions by nicolas-grekas · Pull Request #25410 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[HttpKernel] Fix logging of post-terminate errors/exceptions
  • Loading branch information
nicolas-grekas committed Dec 9, 2017
commit e85b371d722dd56a628bd42923f420272f3a463d
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class DebugHandlersListener implements EventSubscriberInterface
private $scream;
private $fileLinkFormat;
private $firstCall = true;
private $hasTerminatedWithException;

/**
* @param callable|null $exceptionHandler A handler that will be called on Exception
Expand All @@ -60,14 +61,16 @@ public function __construct($exceptionHandler, LoggerInterface $logger = null, $
*/
public function configure(Event $event = null)
{
if (!$this->firstCall) {
if (!$event instanceof KernelEvent ? !$this->firstCall : !$event->isMasterRequest()) {
return;
}
$this->firstCall = false;
$this->firstCall = $this->hasTerminatedWithException = false;

$handler = set_exception_handler('var_dump');
$handler = is_array($handler) ? $handler[0] : null;
restore_exception_handler();

if ($this->logger || null !== $this->throwAt) {
$handler = set_error_handler('var_dump');
$handler = is_array($handler) ? $handler[0] : null;
restore_error_handler();
if ($handler instanceof ErrorHandler) {
if ($this->logger) {
$handler->setDefaultLogger($this->logger, $this->levels);
Expand All @@ -91,8 +94,16 @@ public function configure(Event $event = null)
}
if (!$this->exceptionHandler) {
if ($event instanceof KernelEvent) {
if (method_exists($event->getKernel(), 'terminateWithException')) {
$this->exceptionHandler = array($event->getKernel(), 'terminateWithException');
if (method_exists($kernel = $event->getKernel(), 'terminateWithException')) {
$request = $event->getRequest();
$hasRun = &$this->hasTerminatedWithException;
$this->exceptionHandler = function (\Exception $e) use ($kernel, $request, &$hasRun) {
if ($hasRun) {
throw $e;
}
$hasRun = true;
$kernel->terminateWithException($e, $request);
};
}
} elseif ($event instanceof ConsoleEvent && $app = $event->getCommand()->getApplication()) {
$output = $event->getOutput();
Expand All @@ -105,9 +116,6 @@ public function configure(Event $event = null)
}
}
if ($this->exceptionHandler) {
$handler = set_exception_handler('var_dump');
$handler = is_array($handler) ? $handler[0] : null;
restore_exception_handler();
if ($handler instanceof ErrorHandler) {
$h = $handler->setExceptionHandler('var_dump') ?: $this->exceptionHandler;
$handler->setExceptionHandler($h);
Expand Down
8 changes: 3 additions & 5 deletions src/Symfony/Component/HttpKernel/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,12 @@ public function terminate(Request $request, Response $response)
}

/**
* @throws \LogicException If the request stack is empty
*
* @internal
*/
public function terminateWithException(\Exception $exception)
public function terminateWithException(\Exception $exception, Request $request = null)
{
if (!$request = $this->requestStack->getMasterRequest()) {
throw new \LogicException('Request stack is empty', 0, $exception);
if (!$request = $request ?: $this->requestStack->getMasterRequest()) {
throw $exception;
}

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