Skip to content

Commit a7ffc01

Browse files
committed
[HttpKernel] added logging when an inline fragment cannot be rendered and ignore_errors is on
1 parent 4a5da74 commit a7ffc01

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

Fragment/InlineFragmentRenderer.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use Symfony\Component\HttpFoundation\Response;
1616
use Symfony\Component\HttpKernel\HttpKernelInterface;
1717
use Symfony\Component\HttpKernel\Controller\ControllerReference;
18+
use Symfony\Component\HttpKernel\KernelEvents;
19+
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
20+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1821

1922
/**
2023
* Implements the inline rendering strategy where the Request is rendered by the current HTTP kernel.
@@ -24,15 +27,17 @@
2427
class InlineFragmentRenderer extends RoutableFragmentRenderer
2528
{
2629
private $kernel;
30+
private $dispatcher;
2731

2832
/**
2933
* Constructor.
3034
*
3135
* @param HttpKernelInterface $kernel A HttpKernelInterface instance
3236
*/
33-
public function __construct(HttpKernelInterface $kernel)
37+
public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null)
3438
{
3539
$this->kernel = $kernel;
40+
$this->dispatcher = $dispatcher;
3641
}
3742

3843
/**
@@ -61,6 +66,14 @@ public function render($uri, Request $request, array $options = array())
6166
try {
6267
return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
6368
} catch (\Exception $e) {
69+
// we dispatch the exception event to trigger the logging
70+
// the response that comes back is simply ignored
71+
if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
72+
$event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
73+
74+
$this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
75+
}
76+
6477
// let's clean up the output buffers that were created by the sub-request
6578
while (ob_get_level() > $level) {
6679
ob_get_clean();

Tests/Fragment/InlineFragmentRendererTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\HttpKernel\Controller\ControllerReference;
1515
use Symfony\Component\HttpKernel\HttpKernel;
1616
use Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer;
17+
use Symfony\Component\HttpKernel\KernelEvents;
1718
use Symfony\Component\HttpFoundation\Request;
1819
use Symfony\Component\HttpFoundation\Response;
1920
use Symfony\Component\EventDispatcher\EventDispatcher;
@@ -75,14 +76,20 @@ public function testRenderWithObjectsAsAttributes()
7576
*/
7677
public function testRenderExceptionNoIgnoreErrors()
7778
{
78-
$strategy = new InlineFragmentRenderer($this->getKernel($this->throwException(new \RuntimeException('foo'))));
79+
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
80+
$dispatcher->expects($this->never())->method('dispatch');
81+
82+
$strategy = new InlineFragmentRenderer($this->getKernel($this->throwException(new \RuntimeException('foo'))), $dispatcher);
7983

8084
$this->assertEquals('foo', $strategy->render('/', Request::create('/'))->getContent());
8185
}
8286

8387
public function testRenderExceptionIgnoreErrors()
8488
{
85-
$strategy = new InlineFragmentRenderer($this->getKernel($this->throwException(new \RuntimeException('foo'))));
89+
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
90+
$dispatcher->expects($this->once())->method('dispatch')->with(KernelEvents::EXCEPTION);
91+
92+
$strategy = new InlineFragmentRenderer($this->getKernel($this->throwException(new \RuntimeException('foo'))), $dispatcher);
8693

8794
$this->assertEmpty($strategy->render('/', Request::create('/'), array('ignore_errors' => true))->getContent());
8895
}

0 commit comments

Comments
 (0)