From: Fabien Potencier Date: Sat, 27 Nov 2010 10:34:36 +0000 (+0100) Subject: added file and line to Twig_Error_Runtime exceptions thrown from Twig_Template X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=6c74b60612d4b8661bbd7d39274866634ba70b46;p=web%2Fkonrad%2Ftwig.git added file and line to Twig_Error_Runtime exceptions thrown from Twig_Template --- diff --git a/CHANGELOG b/CHANGELOG index 4ca63c0..91cf679 100644 --- 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 diff --git a/lib/Twig/Error.php b/lib/Twig/Error.php index 98ff328..e9b60c3 100644 --- a/lib/Twig/Error.php +++ b/lib/Twig/Error.php @@ -17,4 +17,43 @@ */ 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); + } + } } diff --git a/lib/Twig/Error/Syntax.php b/lib/Twig/Error/Syntax.php index 7ef10ff..730c75f 100644 --- a/lib/Twig/Error/Syntax.php +++ b/lib/Twig/Error/Syntax.php @@ -18,35 +18,4 @@ */ 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; - } } diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index c761685..5311583 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -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')) {