From 7051d2e598ef89e2d24c3139850c497178f587dd Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 10 Jul 2012 14:40:33 +0200 Subject: [PATCH] fixed exception trace when an error occurs when rendering a child template --- CHANGELOG | 1 + lib/Twig/Error.php | 12 +++----- test/Twig/Tests/ErrorTest.php | 38 +++++++++++++++++++++++++++- test/Twig/Tests/Fixtures/errors/base.html | 1 + test/Twig/Tests/Fixtures/errors/index.html | 7 +++++ 5 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/errors/base.html create mode 100644 test/Twig/Tests/Fixtures/errors/index.html 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 %} -- 1.7.2.5