fixed exception trace when an error occurs when rendering a child template

This commit is contained in:
Fabien Potencier
2012-07-10 14:40:33 +02:00
parent 26eb0a2653
commit 7051d2e598
5 changed files with 51 additions and 8 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.9.0 (2012-XX-XX)
* fixed exception trace when an error occurs when rendering a child template
* added escaping strategies for CSS, URL, and HTML attributes
* fixed nested embed tag calls
* added the date_modify filter
+5 -7
View File
@@ -156,16 +156,14 @@ class Twig_Error extends Exception
foreach (debug_backtrace() as $trace) {
if (isset($trace['object']) && $trace['object'] instanceof Twig_Template && 'Twig_Template' !== get_class($trace['object'])) {
$template = $trace['object'];
// update template filename
if (null === $this->filename) {
$this->filename = $template->getTemplateName();
}
break;
}
}
// update template filename
if (null !== $template && null === $this->filename) {
$this->filename = $template->getTemplateName();
}
if (null === $template || $this->lineno > -1) {
return;
}
+37 -1
View File
@@ -78,7 +78,6 @@ class Twig_Tests_ErrorTest extends PHPUnit_Framework_TestCase
$twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
$template = $twig->loadTemplate('index');
try {
$template->render(array());
@@ -88,6 +87,43 @@ class Twig_Tests_ErrorTest extends PHPUnit_Framework_TestCase
$this->assertEquals(3, $e->getTemplateLine());
$this->assertEquals('index', $e->getTemplateFile());
}
try {
$template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
$this->fail();
} catch (Twig_Error_Runtime $e) {
$this->assertEquals('An exception has been thrown during the rendering of a template ("Runtime error...") in "index" at line 3.', $e->getMessage());
$this->assertEquals(3, $e->getTemplateLine());
$this->assertEquals('index', $e->getTemplateFile());
}
}
public function testTwigExceptionAddsFileAndLineWhenMissingWithInheritanceOnDisk()
{
$loader = new Twig_Loader_Filesystem(__DIR__.'/Fixtures/errors');
$twig = new Twig_Environment($loader, array('strict_variables' => true, 'debug' => true, 'cache' => false));
$template = $twig->loadTemplate('index.html');
try {
$template->render(array());
$this->fail();
} catch (Twig_Error_Runtime $e) {
$this->assertEquals('Variable "foo" does not exist in "index.html" at line 3', $e->getMessage());
$this->assertEquals(3, $e->getTemplateLine());
$this->assertEquals('index.html', $e->getTemplateFile());
}
try {
$template->render(array('foo' => new Twig_Tests_ErrorTest_Foo()));
$this->fail();
} catch (Twig_Error_Runtime $e) {
$this->assertEquals('An exception has been thrown during the rendering of a template ("Runtime error...") in "index.html" at line 3.', $e->getMessage());
$this->assertEquals(3, $e->getTemplateLine());
$this->assertEquals('index.html', $e->getTemplateFile());
}
}
}
@@ -0,0 +1 @@
{% block content %}{% endblock %}
@@ -0,0 +1,7 @@
{% extends 'base.html' %}
{% block content %}
{{ foo.bar }}
{% endblock %}
{% block foo %}
{{ foo.bar }}
{% endblock %}