[WebProfilerBundle] Fix resiliency to exceptions thrown by the url generator

This commit is contained in:
Nicolas Grekas 2015-04-14 10:33:55 +02:00
parent 85a494c12d
commit 92e33e482f
2 changed files with 29 additions and 4 deletions

View File

@ -61,10 +61,14 @@ class WebDebugToolbarListener implements EventSubscriberInterface
$request = $event->getRequest();
if ($response->headers->has('X-Debug-Token') && null !== $this->urlGenerator) {
$response->headers->set(
'X-Debug-Token-Link',
$this->urlGenerator->generate('_profiler', array('token' => $response->headers->get('X-Debug-Token')))
);
try {
$response->headers->set(
'X-Debug-Token-Link',
$this->urlGenerator->generate('_profiler', array('token' => $response->headers->get('X-Debug-Token')))
);
} catch (\Exception $e) {
$response->headers->set('X-Debug-Error', get_class($e).': '.$e->getMessage());
}
}
if (!$event->isMasterRequest()) {

View File

@ -206,6 +206,27 @@ class WebDebugToolbarListenerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('/_profiler/xxxxxxxx', $response->headers->get('X-Debug-Token-Link'));
}
public function testThrowingUrlGenerator()
{
$response = new Response();
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
$urlGenerator = $this->getUrlGeneratorMock();
$urlGenerator
->expects($this->once())
->method('generate')
->with('_profiler', array('token' => 'xxxxxxxx'))
->will($this->throwException(new \Exception('foo')))
;
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, 'bottom', $urlGenerator);
$listener->onKernelResponse($event);
$this->assertEquals('Exception: foo', $response->headers->get('X-Debug-Error'));
}
protected function getRequestMock($isXmlHttpRequest = false, $requestFormat = 'html', $hasSession = true)
{
$request = $this->getMock(