8000 bug #26138 [HttpKernel] Catch HttpExceptions when templating is not i… · nicolas-grekas/symfony@b213c5a · GitHub
[go: up one dir, main page]

Skip to content

Commit b213c5a

Browse files
bug symfony#26138 [HttpKernel] Catch HttpExceptions when templating is not installed (cilefen)
This PR was merged into the 3.4 branch. Discussion ---------- [HttpKernel] Catch HttpExceptions when templating is not installed | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | ? | Deprecations? | no | Tests pass? | ? | Fixed tickets | symfony#25844 | License | MIT | Doc PR | symfony/symfony-docs#... <!--highly recommended for new features--> - [x] Test manually - [x] Check for BC breaks - [x] Needs tests <!-- - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. - Replace this comment by a description of what your PR is solving. --> Commits ------- 4e527aa bug symfony#25844 [HttpKernel] Catch HttpExceptions when templating is not installed
2 parents 61af0e3 + 4e527aa commit b213c5a

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@
6767
<argument type="service" id="router" on-invalid="ignore" />
6868
</service>
6969

70+
<service id="http_exception_listener" class="Symfony\Component\HttpKernel\EventListener\ExceptionListener">
71+
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="-256"/>
72+
<tag name="monolog.logger" channel="request" />
73+
<argument>null</argument>
74+
<argument type="service" id="logger" on-invalid="null" />
75+
<ar 8000 gument>%kernel.debug%</argument>
76+
<argument>%kernel.charset%</argument>
77+
</service>
78+
7079
<service id="validate_request_listener" class="Symfony\Component\HttpKernel\EventListener\ValidateRequestListener">
7180
<tag name="kernel.event_subscriber" />
7281
</service>

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
namespace Symfony\Component\HttpKernel\EventListener;
1313

1414
use Psr\Log\LoggerInterface;
15+
use Symfony\Component\Debug\ExceptionHandler;
1516
use Symfony\Component\Debug\Exception\FlattenException;
1617
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1718
use Symfony\Component\HttpFoundation\Request;
19+
use Symfony\Component\HttpFoundation\Response;
1820
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
1921
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
2022
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
@@ -33,12 +35,14 @@ class ExceptionListener implements EventSubscriberInterface
3335
protected $controller;
3436
protected $logger;
3537
protected $debug;
38+
private $charset;
3639

37-
public function __construct($controller, LoggerInterface $logger = null, $debug = false)
40+
public function __construct($controller, LoggerInterface $logger = null, $debug = false, $charset = null)
3841
{
3942
$this->controller = $controller;
4043
$this->logger = $logger;
4144
$this->debug = $debug;
45+
$this->charset = $charset;
4246
}
4347

4448
public function onKernelException(GetResponseForExceptionEvent $event)
@@ -117,8 +121,12 @@ protected function logException(\Exception $exception, $message)
117121
protected function duplicateRequest(\Exception $exception, Request $request)
118122
{
119123
$attributes = array(
120-
'_controller' => $this->controller,
121-
'exception' => FlattenException::create($exception),
124+
'exception' => $exception = FlattenException::create($exception),
125+
'_controller' => $this->controller ?: function () use ($exception) {
126+
$handler = new ExceptionHandler($this->debug, $this->charset);
127+
128+
return new Response($handler->getHtml($exception), $exception->getStatusCode(), $exception->getHeaders());
129+
},
122130
'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
123131
);
124132
$request = $request->duplicate(null, null, $attributes);

src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,23 @@ public function testCSPHeaderIsRemoved()
151151
$this->assertFalse($response->headers->has('content-security-policy'), 'CSP header has been removed');
152152
$this->assertFalse($dispatcher->hasListeners(KernelEvents::RESPONSE), 'CSP removal listener has been removed');
153153
}
154+
155+
public function testNullController()
156+
{
157+
$listener = new ExceptionListener(null);
158+
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
159+
$kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) {
160+
$controller = $request->attributes->get('_controller');
161+
162+
return $controller();
163+
}));
164+
$request = Request::create('/');
165+
$event = new GetResponseForExceptionEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, new \Exception('foo'));
166+
167+
$listener->onKernelException($event);
168+
169+
$this->assertContains('Whoops, looks like something went wrong.', $event->getResponse()->getContent());
170+
}
154171
}
155172

156173
class TestLogger extends Logger implements DebugLoggerInterface

0 commit comments

Comments
 (0)
0