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
[Twig] Added an exception processor for TwigError
  • Loading branch information
hason committed Sep 17, 2015
commit 0f558bcd1ca2cb75855d04b03735f6c3341861f0
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

2.8.0
-----

* added TwigFlattenExceptionProcessor which adds twig files into the FlattenException

2.7.0
-----

Expand Down
140 changes: 140 additions & 0 deletions src/Symfony/Bridge/Twig/Debug/TwigFlattenExceptionProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?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\Bridge\Twig\Debug;

use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\FlattenExceptionProcessorInterface;

/**
* TwigFlattener adds twig files into FlattenException.
*
* @author Martin Hasoň <martin.hason@gmail.com>
*/
class TwigFlattenExceptionProcessor implements FlattenExceptionProcessorInterface
{
private $files = array();
private $twig;
private $loadedTemplates;

public function __construct(\Twig_Environment $twig)
{
$this->twig = $twig;
$this->loadedTemplates = new \ReflectionProperty($twig, 'loadedTemplates');
$this->loadedTemplates->setAccessible(true);
}

/**
* {@inheritdoc}
*/
public function process(\Exception $exception, FlattenException $flattenException, $master)
{
$trace = $flattenException->getTrace();
$origTrace = $exception->getTrace();

foreach ($origTrace as $key => $entry) {
$prevKey = $key - 1;

if (!isset($origTrace[$prevKey]) || !isset($entry['class']) || 'Twig_Template' === $entry['class'] || !is_subclass_of($entry['class'], 'Twig_Template')) {
continue;
}

$template = $this->getLoadedTemplate($entry['class']);

if (!$template instanceof \Twig_Template) {
continue;
}

$file = $this->findOrCreateFile($template);

$data = array('file' => $file);
if (isset($origTrace[$prevKey]['line'])) {
$data['line'] = $this->findLineInTemplate($origTrace[$prevKey]['line'], $template);
}

$trace[$prevKey]['related_codes'][] = $data;
}

if (isset($trace[-1]) && $exception instanceof \Twig_Error) {
$name = $exception->getTemplateFile();
$file = $this->findOrCreateFile($name, $name);

$trace[-1]['related_codes'][] = array('file' => $file, 'line' => $exception->getTemplateLine());
}

$flattenException->replaceTrace($trace);
}

private function getLoadedTemplate($class)
{
$loadedTemplates = $this->loadedTemplates->getValue($this->twig);

return isset($loadedTemplates[$class]) ? $loadedTemplates[$class] : null;
}

private function findOrCreateFile($template, $path = null)
{
$name = $template instanceof \Twig_Template ? $template->getTemplateName() : $template;

if (isset($this->files[$name])) {
return $this->files[$name];
}

foreach ($this->files as $key => $file) {
if (isset($file->path) && $file->path == $name) {
return $file;
}
}

$file = (object) array('name' => $name, 'type' => 'twig');

try {
$path = $path ?: $this->twig->getLoader()->getCacheKey($name);
} catch (\Twig_Error_Loader $e) {
}

if (is_file($path)) {
$file->path = $path;
} else {
$source = null;

if (method_exists($template, 'getSource')) {
$source = $template->getSource();
}

if (null === $source) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move this if as the else of the previous one (and remove $source = null initialization)

try {
$source = $this->twig->getLoader()->getSource($name);
} catch (\Twig_Error_Loader $e) {
}
}

$file->content = $source;
}

return $this->files[$name] = $file;
}

private function findLineInTemplate($line, $template)
{
if (!method_exists($template, 'getDebugInfo')) {
return 1;
}

foreach ($template->getDebugInfo() as $codeLine => $templateLine) {
if ($codeLine <= $line) {
return $templateLine;
}
}

return 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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\Bridge\Twig\Tests;

use Symfony\Bridge\Twig\Debug\TwigFlattenExceptionProcessor;
use Symfony\Component\Debug\ExceptionFlattener;

class TwigFlattenExceptionProcessorTest extends \PHPUnit_Framework_TestCase
{
public function testProcess()
{
$contents = array(
'base' => "{% block content '' %}",
'layout' => "{% extends 'base' %}\n{% block content %}\nfoo\n{{ foo.foo }}\n{% endblock %}",
'index' => "{% extends 'layout' %}\n{% block content %}\n{{ parent() }}\n{% endblock %}",
);

$files = array(
'base' => (object) array('name' => 'base', 'content' => $contents['base'], 'type' => 'twig'),
'layout' => (object) array('name' => 'layout', 'content' => $contents['layout'], 'type' => 'twig'),
'index' => (object) array('name' => 'index', 'content' => $contents['index'], 'type' => 'twig'),
);

$twig = new \Twig_Environment(new \Twig_Loader_Array($contents), array('strict_variables' => true));

try {
$twig->render('index', array('foo' => 'foo'));
} catch (\Twig_Error $exception) {
}

$flattener = new ExceptionFlattener(array(new TwigFlattenExceptionProcessor($twig)));
$trace = $flattener->flatten($exception)->getTrace();

$this->assertEquals(array(array('line' => 4, 'file' => $files['layout'])), $trace[-1]['related_codes']);
$this->assertEquals(array(array('line' => 4, 'file' => $files['layout'])), $trace[0]['related_codes']);
$this->assertEquals(array(array('line' => 3, 'file' => $files['index'])), $trace[3]['related_codes']);
$this->assertEquals(array(array('line' => 1, 'file' => $files['base'])), $trace[5]['related_codes']);
$this->assertEquals(array(array('line' => 1, 'file' => $files['layout'])), $trace[8]['related_codes']);
$this->assertEquals(array(array('line' => 1, 'file' => $files['index'])), $trace[11]['related_codes']);
97AF }

public function testProcessRealTwigFile()
{
$file = (object) array(
'name' => 'error.html.twig',
'path' => dirname(__DIR__).'/Fixtures/templates/error.html.twig',
'type' => 'twig',
);

$twig = new \Twig_Environment(new \Twig_Loader_Filesystem(array(dirname($file->path))), array('strict_variables' => true));

try {
$twig->render($file->name, array('foo' => 'foo'));
} catch (\Twig_Error $exception) {
}

$flattener = new ExceptionFlattener(array(new TwigFlattenExceptionProcessor($twig)));
$trace = $flattener->flatten($exception)->getTrace();

$this->assertEquals(array(array('line' => 2, 'file' => $file)), $trace[-1]['related_codes']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo
{{ foo.foo }}
4 changes: 3 additions & 1 deletion src/Symfony/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"symfony/stopwatch": "~2.2|~3.0.0",
"symfony/console": "~2.7|~3.0.0",
"symfony/var-dumper": "~2.6|~3.0.0",
"symfony/expression-language": "~2.4|~3.0.0"
"symfony/expression-language": "~2.4|~3.0.0",
"symfony/debug": "~2.6|~3.0.0"
},
"suggest": {
"symfony/finder": "",
Expand All @@ -48,6 +49,7 @@
"symfony/yaml": "For using the YamlExtension",
"symfony/security": "For using the SecurityExtension",
"symfony/stopwatch": "For using the StopwatchExtension",
"symfony/debug": "For using the TwigFlattenExceptionProcessor",
"symfony/var-dumper": "For using the DumpExtension",
"symfony/expression-language": "For using the ExpressionExtension"
},
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
5953
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,10 @@
<argument /> <!-- decimal point, set in TwigExtension -->
<argument /> <!-- thousands separator, set in TwigExtension -->
</service>

<service id="debug.exception_processor.twig" class="Symfony\Bridge\Twig\Debug\TwigFlattenExceptionProcessor" public="false">
<tag name="exception.processor" />
<argument type="service" id="twig" />
</service>
</services>
</container>
0