8000 [WIP] Better exception page by hason · Pull Request #15792 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[WIP] Better exception page #15792

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Integrated the ExceptionFlattener into components and bundles
  • Loading branch information
hason committed Sep 17, 2015
commit bab9b5c74e21fb6bd304a15e4843b824cbcdd517
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/DebugBundle/DebugBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\DebugBundle;

use Symfony\Bundle\DebugBundle\DependencyInjection\Compiler\AddFlattenExceptionProcessorPass;
use Symfony\Bundle\DebugBundle\DependencyInjection\Compiler\DumpDataCollectorPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand Down Expand Up @@ -48,6 +49,7 @@ public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new AddFlattenExceptionProcessorPass());
$container->addCompilerPass(new DumpDataCollectorPass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\DebugBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Reference;

/**
* Registers the exception processors.
*
* @author Martin Hasoň <martin.hason@gmail.com>
*/
class AddFlattenExceptionProcessorPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('exception_flattener')) {
return;
}

$processors = array();
foreach ($container->findTaggedServiceIds('exception.processor') as $id => $tags) {
$priority = isset($tags[0]['priority']) ? $tags[0]['priority'] : 0;
$processors[$priority][] = new Reference($id);
}

if (empty($processors)) {
return;
}

// sort by priority and flatten
krsort($processors);
$processors = call_user_func_array('array_merge', $processors);

$container->getDefinition('exception_flattener')->replaceArgument(0, $processors);
}
}
8000
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/DebugBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<argument>null</argument><!-- debug.dump_destination -->
<argument>%kernel.charset%</argument>
</service>

<service id="exception_flattener" class="Symfony\Component\Debug\ExceptionFlattener">
<argument type="collection" />
</service>
</services>

</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\DebugBundle\Tests\DependencyInjection\Compiler;

use Symfony\Bundle\DebugBundle\DependencyInjection\Compiler\AddFlattenExceptionProcessorPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class AddFlattenExceptionProcessorPassTest extends \PHPUnit_Framework_TestCase
{
public function testProcess()
{
$container = new ContainerBuilder();
$container->addCompilerPass(new AddFlattenExceptionProcessorPass());

$definition = new Definition('Symfony\Component\Debug\ExceptionFlattener', array(array()));
$container->setDefinition('exception_flattener', $definition);

$processor1 = new Definition('Symfony\Component\Debug\FlattenExceptionProcessorInterface');
$processor1->addTag('exception.processor', array('priority' => -100));

$processor2 = new Definition('Symfony\Component\Debug\FlattenExceptionProcessorInterface');
$processor2->addTag('exception.processor', array('priority' => 100));

$container->setDefinition('processor_1', $processor1);
$container->setDefinition('processor_2', $processor2);

$container->compile();

$this->assertEquals(array(array(new Reference('processor_2'), new Reference('processor_1'))), $definition->getArguments());
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/DebugBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"php": ">=5.3.9",
"symfony/http-kernel": "~2.6|~3.0.0",
"symfony/twig-bridge": "~2.6|~3.0.0",
"symfony/var-dumper": "~2.6|~3.0.0"
"symfony/var-dumper": "~2.6|~3.0.0",
"symfony/debug": "~2.6|~3.0.0"
},
"require-dev": {
"symfony/phpunit-bridge": "~2.7|~3.0.0",
Expand Down
9E88
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

<service id="data_collector.exception" class="%data_collector.exception.class%" public="false">
<tag name="data_collector" template="@WebProfiler/Collector/exception.html.twig" id="exception" priority="255" />
<argument type="service" id="exception_flattener" on-invalid="ignore" />
</service>

<service id="data_collector.events" class="%data_collector.events.class%" public="false">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\TwigBundle\Controller;

use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\ExceptionFlattener;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -26,16 +27,20 @@ class PreviewErrorController
{
protected $kernel;
protected $controller;
protected $flattener;

public function __construct(HttpKernelInterface $kernel, $controller)
public function __construct(HttpKernelInterface $kernel, $controller, ExceptionFlattener $flattener = null)
{
$this->kernel = $kernel;
$this->controller = $controller;
$this->flattener = $flattener;
}

public function previewErrorPageAction(Request $request, $code)
{
$exception = FlattenException::create(new \Exception('Something has intentionally gone wrong.'), $code);
$e = new \Exception('Something has intentionally gone wrong.');
$exception = null === $this->flattener ? FlattenException::create($e, $code) : $this->flattener->flatten($e);
$exception->setStatusCode($code);

/*
* This Request mimics the parameters set by
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<tag name="monolog.logger" channel="request" />
<argument>%twig.exception_listener.controller%</argument>
<argument type="service" id="logger" on-invalid="null" />
<argument type="service" id="exception_flattener" on-invalid="null" />
</service>

<service id="twig.controller.exception" class="%twig.controller.exception.class%">
Expand Down
9 changes: 5 additions & 4 deletions src/Symfony/Component/Debug/ExceptionHandler.php
10000
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ExceptionHandler
private $caughtLength;
private $fileLinkFormat;

public function __construct($debug = true, $charset = null, $fileLinkFormat = null)
public function __construct($debug = true, $charset = null, $fileLinkFormat = null, ExceptionFlattener $flattener = null)
{
if (false !== strpos($charset, '%')) {
// Swap $charset and $fileLinkFormat for BC reasons
Expand All @@ -47,6 +47,7 @@ public function __construct($debug = true, $charset = null, $fileLinkFormat = nu
$this->debug = $debug;
$this->charset = $charset ?: ini_get('default_charset') ?: 'UTF-8';
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
$this->flattener = $flattener;
}

/**
Expand Down Expand Up @@ -183,7 +184,7 @@ private function failSafeHandle(\Exception $exception)
public function sendPhpResponse($exception)
{
if (!$exception instanceof FlattenException) {
$exception = FlattenException::create($exception);
$exception = null !== $this->flattener ? $this->flattener->flatten($exception) : FlattenException::create($exception);
}

if (!headers_sent()) {
Expand Down Expand Up @@ -211,7 +212,7 @@ public function createResponse($exception)
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);

if (!$exception instanceof FlattenException) {
$exception = FlattenException::create($exception);
$exception = null !== $this->flattener ? $this->flattener->flatten($exception) : FlattenException::create($exception);
}

return Response::create($this->getHtml($exception), $exception->getStatusCode(), $exception->getHeaders())->setCharset($this->charset);
Expand All @@ -227,7 +228,7 @@ public function createResponse($exception)
public function getHtml($exception)
{
if (!$exception instanceof FlattenException) {
$exception = FlattenException::create($exception);
$exception = null !== $this->flattener ? $this->flattener->flatten($exception) : FlattenException::create($exception);
}

return $this->decorate($this->getContent($exception), $this->getStylesheet($exception));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\DataCollector;

use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\ExceptionFlattener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -22,16 +23,25 @@
*/
class ExceptionDataCollector extends DataCollector
{
private $flattener;

public function __construct(ExceptionFlattener $flattener = null)
{
$this->flattener = $flattener;
}

/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
if (null !== $exception) {
$this->data = array(
'exception' => FlattenException::create($exception),
);
if (null === $exception) {
return;
}

$this->data = array(
'exception' => null === $this->flattener ? FlattenException::create($exception) : $this->flattener->flatten($exception),
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Psr\Log\LoggerInterface;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\ExceptionFlattener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
Expand All @@ -30,11 +31,13 @@ class ExceptionListener implements EventSubscriberInterface
{
protected $controller;
protected $logger;
protected $flattener;

public function __construct($controller, LoggerInterface $logger = null)
public function __construct($controller, LoggerInterface $logger = null, ExceptionFlattener $flattener = null)
{
$this->controller = $controller;
$this->logger = $logger;
$this->flattener = $flattener;
}

public function onKernelException(GetResponseForExceptionEvent $event)
Expand Down Expand Up @@ -103,9 +106,11 @@ protected function logException(\Exception $exception, $message)
*/
protected function duplicateRequest(\Exception $exception, Request $request)
{
$flattenException = null === $this->flattener ? FlattenException::create($exception) : $this->flattener->flatten($exception);

$attributes = array(
'_controller' => $this->controller,
'exception' => FlattenException::create($exception),
'exception' => $flattenException,
'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
// keep for BC -- as $format can be an argument of the controller callable
// see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
namespace Symfony\Component\HttpKernel\Tests\DataCollector;

use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\ExceptionFlattener;
use Symfony\Component\HttpKernel\DataCollector\ExceptionDataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
1241
class ExceptionDataCollectorTest extends \PHPUnit_Framework_TestCase
{
public function testCollect()
/**
* @dataProvider getData
*/
public function testCollect($c, $e, $flattened)
{
$e = new \Exception('foo', 500);
$c = new ExceptionDataCollector();
$flattened = FlattenException::create($e);
$trace = $flattened->getTrace();

$this->assertFalse($c->hasException());
Expand All @@ -34,6 +35,17 @@ public function testCollect()
$this->assertSame('foo', $c->getMessage());
$this->assertSame(500, $c->getCode());
$this->assertSame('exception', $c->getName());
$this->assertSame($trace, $c->getTrace());
$this->assertEquals($trace, $c->getTrace());
}

public function getData()
{
$e = new \Exception('foo', 500);
$flattener = new ExceptionFlattener();

return array(
array(new ExceptionDataCollector(), $e, FlattenException::create($e)),
array(new ExceptionDataCollector($flattener), $e, $flattener->flatten($e)),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ public function testSubRequestFormat()
$response = $event->getResponse();
$this->assertEquals('xml', $response->getContent());
}

public function testUseFlattener()
{
$exception = new \Exception('foo');

$flattener = $this->getMock('Symfony\Component\Debug\ExceptionFlattener');
$flattener->expects($this->once())->method('flatten')->with($exception);

$listener = new ExceptionListener('foo', null, $flattener);

$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$kernel->expects($this->once())->method('handle')->will($this->returnCallback(function () {
return new Response();
}));

$request = Request::create('/');
$event = new GetResponseForExceptionEvent($kernel, $request, 'foo', $exception);
$listener->onKernelException($event);
}
}

class TestLogger extends Logger implements DebugLoggerInterface
Expand Down
0