From: Fabien Potencier Date: Tue, 10 Jul 2012 12:40:33 +0000 (+0200) Subject: fixed exception trace when an error occurs when rendering a child template X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=7051d2e598ef89e2d24c3139850c497178f587dd;p=web%2Fkonrad%2Ftwig.git fixed exception trace when an error occurs when rendering a child template --- diff --git a/CHANGELOG b/CHANGELOG index e9286a9..0b9d53d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/lib/Twig/Error.php b/lib/Twig/Error.php index 4b74e40..b5ac00e 100644 --- a/lib/Twig/Error.php +++ b/lib/Twig/Error.php @@ -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; } diff --git a/test/Twig/Tests/ErrorTest.php b/test/Twig/Tests/ErrorTest.php index d0e48be..33ca43d 100644 --- a/test/Twig/Tests/ErrorTest.php +++ b/test/Twig/Tests/ErrorTest.php @@ -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()); + } } } diff --git a/test/Twig/Tests/Fixtures/errors/base.html b/test/Twig/Tests/Fixtures/errors/base.html new file mode 100644 index 0000000..cb0dbe4 --- /dev/null +++ b/test/Twig/Tests/Fixtures/errors/base.html @@ -0,0 +1 @@ +{% block content %}{% endblock %} diff --git a/test/Twig/Tests/Fixtures/errors/index.html b/test/Twig/Tests/Fixtures/errors/index.html new file mode 100644 index 0000000..df57c82 --- /dev/null +++ b/test/Twig/Tests/Fixtures/errors/index.html @@ -0,0 +1,7 @@ +{% extends 'base.html' %} +{% block content %} + {{ foo.bar }} +{% endblock %} +{% block foo %} + {{ foo.bar }} +{% endblock %}