Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[HttpKernel] Fix handling non-catchable fatal errors #31868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2019
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 handling non-catchable fatal errors
This reverts PR #27519 this commit 18c2dde,
reversing changes made to ac1189a.
  • Loading branch information
nicolas-grekas committed Jun 5, 2019
commit a1619ccb955965c47b659151d245c6c4ba5be507
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,6 @@ private function registerDebugConfiguration(array $config, ContainerBuilder $con
$container->setParameter('debug.error_handler.throw_at', 0);
}

$definition->replaceArgument(4, $debug);
$definition->replaceArgument(6, $debug);

if ($debug && class_exists(DebugProcessor::class)) {
$definition = new Definition(DebugProcessor::class);
$definition->setPublic(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
<argument type="service" id="logger" on-invalid="null" />
<argument>null</argument><!-- Log levels map for enabled error levels -->
<argument>%debug.error_handler.throw_at%</argument>
<argument>true</argument>
<argument>%kernel.debug%</argument>
<argument type="service" id="debug.file_link_formatter"></argument>
<argument>true</argument>
<argument>%kernel.debug%</argument>
<argument>%kernel.charset%</argument>
</service>

<service id="debug.file_link_formatter" class="Symfony\Component\HttpKernel\Debug\FileLinkFormatter">
Expand Down
10 changes: 0 additions & 10 deletions 10 src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,6 @@
<argument type="service" id="router" on-invalid="ignore" />
</service>

<service id="http_exception_listener" class="Symfony\Component\HttpKernel\EventListener\ExceptionListener">
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="-2048" />
<tag name="kernel.reset" method="reset" />
<argument>null</argument>
<argument>null</argument>
<argument>%kernel.debug%</argument>
<argument>%kernel.charset%</argument>
<argument>%debug.file_link_format%</argument>
</service>

<service id="validate_request_listener" class="Symfony\Component\HttpKernel\EventListener\ValidateRequestListener">
<tag name="kernel.event_subscriber" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\KernelEvents;

Expand All @@ -37,6 +40,7 @@ class DebugHandlersListener implements EventSubscriberInterface
private $scream;
private $fileLinkFormat;
private $scope;
private $charset;
private $firstCall = true;
private $hasTerminatedWithException;

Expand All @@ -49,7 +53,7 @@ class DebugHandlersListener implements EventSubscriberInterface
* @param string|FileLinkFormatter|null $fileLinkFormat The format for links to source files
* @param bool $scope Enables/disables scoping mode
*/
public function __construct(callable $exceptionHandler = null, LoggerInterface $logger = null, $levels = E_ALL, ?int $throwAt = E_ALL, bool $scream = true, $fileLinkFormat = null, bool $scope = true)
public function __construct(callable $exceptionHandler = null, LoggerInterface $logger = null, $levels = E_ALL, ?int $throwAt = E_ALL, bool $scream = true, $fileLinkFormat = null, bool $scope = true, string $charset = null)
{
$this->exceptionHandler = $exceptionHandler;
$this->logger = $logger;
Expand All @@ -58,6 +62,7 @@ public function __construct(callable $exceptionHandler = null, LoggerInterface $
$this->scream = $scream;
$this->fileLinkFormat = $fileLinkFormat;
$this->scope = $scope;
$this->charset = $charset;
}

/**
Expand Down Expand Up @@ -144,6 +149,26 @@ public function configure(Event $event = null)
}
}

/**
* @internal
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
if (!$this->hasTerminatedWithException || !$event->isMasterRequest()) {
return;
}

$debug = $this->scream && $this->scope;
$controller = function (Request $request) use ($debug) {
$e = $request->attributes->get('exception');
$handler = new ExceptionHandler($debug, $this->charset, $this->fileLinkFormat);

return new Response($handler->getHtml($e), $e->getStatusCode(), $e->getHeaders());
};

(new ExceptionListener($controller, $this->logger, $debug))->onKernelException($event);
}

public static function getSubscribedEvents()
{
$events = [KernelEvents::REQUEST => ['configure', 2048]];
Expand All @@ -152,6 +177,8 @@ public static function getSubscribedEvents()
$events[ConsoleEvents::COMMAND] = ['configure', 2048];
}

$events[KernelEvents::EXCEPTION] = ['onKernelException', -2048];

return $events;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@

use Psr\Log\LoggerInterface;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
Expand All @@ -35,17 +33,12 @@ class ExceptionListener implements EventSubscriberInterface
protected $controller;
protected $logger;
protected $debug;
private $charset;
private $fileLinkFormat;
private $isTerminating = false;

public function __construct($controller, LoggerInterface $logger = null, $debug = false, string $charset = null, $fileLinkFormat = null)
public function __construct($controller, LoggerInterface $logger = null, $debug = false)
{
$this->controller = $controller;
$this->logger = $logger;
$this->debug = $debug;
$this->charset = $charset;
$this->fileLinkFormat = $fileLinkFormat;
}

public function logKernelException(GetResponseForExceptionEvent $event)
Expand All @@ -58,16 +51,9 @@ public function logKernelException(GetResponseForExceptionEvent $event)
public function onKernelException(GetResponseForExceptionEvent $event)
{
if (null === $this->controller) {
if (!$event->isMasterRequest()) {
return;
}
if (!$this->isTerminating) {
$this->isTerminating = true;

return;
}
$this->isTerminating = false;
return;
}

$exception = $event->getException();
$request = $this->duplicateRequest($exception, $event->getRequest());
$eventDispatcher = \func_num_args() > 2 ? func_get_arg(2) : null;
Expand Down Expand Up @@ -104,11 +90,6 @@ public function onKernelException(GetResponseForExceptionEvent $event)
}
}

public function reset()
{
$this->isTerminating = false;
}

public static function getSubscribedEvents()
{
return [
Expand Down Expand Up @@ -147,12 +128,8 @@ protected function logException(\Exception $exception, $message)
protected function duplicateRequest(\Exception $exception, Request $request)
{
$attributes = [
'exception' => $exception = FlattenException::create($exception),
'_controller' => $this->controller ?: function () use ($exception) {
$handler = new ExceptionHandler($this->debug, $this->charset, $this->fileLinkFormat);

return new Response($handler->getHtml($exception), $exception->getStatusCode(), $exception->getHeaders());
},
'_controller' => $this->controller,
'exception' => FlattenException::create($exception),
'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
];
$request = $request->duplicate(null, null, $attributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public function testConsoleEvent()
$xListeners = [
KernelEvents::REQUEST => [[$listener, 'configure']],
ConsoleEvents::COMMAND => [[$listener, 'configure']],
KernelEvents::EXCEPTION => [[$listener, 'onKernelException']],
];
$this->assertSame($xListeners, $dispatcher->getListeners());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,6 @@ public function testCSPHeaderIsRemoved()
$this->assertFalse($response->headers->has('content-security-policy'), 'CSP header has been removed');
$this->assertFalse($dispatcher->hasListeners(KernelEvents::RESPONSE), 'CSP removal listener has been removed');
}

public function testNullController()
{
$listener = new ExceptionListener(null);
$kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
$kernel->expects($this->once())->method('handle')->willReturnCallback(function (Request $request) {
$controller = $request->attributes->get('_controller');

return $controller();
});
$request = Request::create('/');
$event = new GetResponseForExceptionEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, new \Exception('foo'));

$listener->onKernelException($event);
$this->assertNull($event->getResponse());

$listener->onKernelException($event);
$this->assertContains('Whoops, looks like something went wrong.', $event->getResponse()->getContent());
}
}

class TestLogger extends Logger implements DebugLoggerInterface
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.