added file and line to Twig_Error_Runtime exceptions thrown from Twig_Template
authorFabien Potencier <fabien.potencier@gmail.com>
Sat, 27 Nov 2010 10:34:36 +0000 (11:34 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Sat, 27 Nov 2010 10:34:36 +0000 (11:34 +0100)
CHANGELOG
lib/Twig/Error.php
lib/Twig/Error/Syntax.php
lib/Twig/Template.php

index 4ca63c0..91cf679 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -15,7 +15,7 @@ Backward incompatibilities:
  * removed support for {{ 1 < i < 3 }} (use {{ i > 1 and i < 3 }} instead)
 
 Changes:
- * made error message for strict variables more useful
+ * added file and line to Twig_Error_Runtime exceptions thrown from Twig_Template
  * changed trans tag to accept any variable for the plural count
  * fixed sandbox mode (__toString() method check was not enforced if called implicitly from complex statements)
  * added the ** (power) operator
index 98ff328..e9b60c3 100644 (file)
  */
 class Twig_Error extends Exception
 {
+    protected $lineno;
+    protected $filename;
+    protected $rawMessage;
+
+    public function __construct($message, $lineno = -1, $filename = 'n/a')
+    {
+        $this->lineno = $lineno;
+        $this->filename = $filename;
+        $this->rawMessage = $message;
+
+        $this->updateRepr();
+
+        parent::__construct($this->message);
+    }
+
+    public function getFilename()
+    {
+        return $this->filename;
+    }
+
+    public function setFilename($filename)
+    {
+        $this->filename = $filename;
+
+        $this->updateRepr();
+    }
+
+    protected function updateRepr()
+    {
+        $this->message = $this->rawMessage;
+
+        if ('n/a' != $this->filename) {
+            $this->message .= sprintf(' in %s', $this->filename);
+        }
+
+        if ($this->lineno >= 0) {
+            $this->message .= sprintf(' at line %d', $this->lineno);
+        }
+    }
 }
index 7ef10ff..730c75f 100644 (file)
  */
 class Twig_Error_Syntax extends Twig_Error
 {
-    protected $lineno;
-    protected $filename;
-    protected $rawMessage;
-
-    public function __construct($message, $lineno, $filename = null)
-    {
-        $this->lineno = $lineno;
-        $this->filename = $filename;
-        $this->rawMessage = $message;
-
-        $this->updateRepr();
-
-        parent::__construct($this->message, $lineno);
-    }
-
-    public function getFilename()
-    {
-        return $this->filename;
-    }
-
-    public function setFilename($filename)
-    {
-        $this->filename = $filename;
-
-        $this->updateRepr();
-    }
-
-    protected function updateRepr()
-    {
-        $this->message = $this->rawMessage.' in '.($this->filename ? $this->filename : 'n/a').' at line '.$this->lineno;
-    }
 }
index c761685..5311583 100644 (file)
@@ -42,7 +42,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
         if (false !== $parent = $this->getParent($context)) {
             return $parent->getBlock($name, $context, $blocks);
         } else {
-            throw new Twig_Error_Runtime('This template has no parent.');
+            throw new Twig_Error_Runtime('This template has no parent', -1, $this->getTemplateName());
         }
     }
 
@@ -95,7 +95,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
     protected function getContext($context, $item, $line = -1)
     {
         if (!array_key_exists($item, $context)) {
-            throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist in "%s" at line %s.', $item, $this->getTemplateName(), $line));
+            throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item), $line, $this->getTemplateName());
         }
 
         return $context[$item];
@@ -114,7 +114,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
                     return null;
                 }
 
-                throw new Twig_Error_Runtime(sprintf('Key "%s" for array "%s" does not exist.', $item, $object));
+                throw new Twig_Error_Runtime(sprintf('Key "%s" for array "%s" does not exist', $item, $object), -1, $this->getTemplateName());
             }
         }
 
@@ -122,7 +122,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
             if (!$this->env->isStrictVariables() || $noStrictCheck) {
                 return null;
             }
-            throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist.', $item, $object));
+            throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object), -1, $this->getTemplateName());
         }
 
         // get some information about the object
@@ -165,7 +165,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
                 return null;
             }
 
-            throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist.', $item, get_class($object)));
+            throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)), -1, $this->getTemplateName());
         }
 
         if ($this->env->hasExtension('sandbox')) {