Skip to content

Commit

Permalink
[HttpKernel] Turn HTTP exceptions to responses on terminateWithExcept…
Browse files Browse the repository at this point in the history
…ion()
  • Loading branch information
nicolas-grekas committed Jun 6, 2018
1 parent 9a077ca commit d5c882f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml
Expand Up @@ -68,8 +68,9 @@
</service>

<service id="http_exception_listener" class="Symfony\Component\HttpKernel\EventListener\ExceptionListener">
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="-256"/>
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="-2048" />
<tag name="monolog.logger" channel="request" />
<tag name="kernel.reset" method="reset" />
<argument>null</argument>
<argument type="service" id="logger" on-invalid="null" />
<argument>%kernel.debug%</argument>
Expand Down
Expand Up @@ -37,6 +37,7 @@ class ExceptionListener implements EventSubscriberInterface
protected $debug;
private $charset;
private $fileLinkFormat;
private $isTerminating = false;

public function __construct($controller, LoggerInterface $logger = null, $debug = false, $charset = null, $fileLinkFormat = null)
{
Expand All @@ -49,6 +50,17 @@ public function __construct($controller, LoggerInterface $logger = null, $debug

public function onKernelException(GetResponseForExceptionEvent $event)
{
if (null === $this->controller) {
if (!$event->isMasterRequest()) {
return;
}
if (!$this->isTerminating) {
$this->isTerminating = true;

return;
}
$this->isTerminating = false;
}
$exception = $event->getException();
$request = $event->getRequest();
$eventDispatcher = func_num_args() > 2 ? func_get_arg(2) : null;
Expand Down Expand Up @@ -88,6 +100,11 @@ public function onKernelException(GetResponseForExceptionEvent $event)
}
}

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

public static function getSubscribedEvents()
{
return array(
Expand Down
Expand Up @@ -165,7 +165,9 @@ public function testNullController()
$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());
}
}
Expand Down

0 comments on commit d5c882f

Please sign in to comment.