fixed exception trace when an error occurs when rendering a child template
authorFabien Potencier <fabien.potencier@gmail.com>
Tue, 10 Jul 2012 12:40:33 +0000 (14:40 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Tue, 10 Jul 2012 12:41:24 +0000 (14:41 +0200)
CHANGELOG
lib/Twig/Error.php
test/Twig/Tests/ErrorTest.php
test/Twig/Tests/Fixtures/errors/base.html [new file with mode: 0644]
test/Twig/Tests/Fixtures/errors/index.html [new file with mode: 0644]

index e9286a9..0b9d53d 100644 (file)
--- 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
index 4b74e40..b5ac00e 100644 (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;
         }
index d0e48be..33ca43d 100644 (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());
+        }
     }
 }
 
diff --git a/test/Twig/Tests/Fixtures/errors/base.html b/test/Twig/Tests/Fixtures/errors/base.html
new file mode 100644 (file)
index 0000000..cb0dbe4
--- /dev/null
@@ -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 (file)
index 0000000..df57c82
--- /dev/null
@@ -0,0 +1,7 @@
+{% extends 'base.html' %}
+{% block content %}
+    {{ foo.bar }}
+{% endblock %}
+{% block foo %}
+    {{ foo.bar }}
+{% endblock %}