From 0a908fb6a19be42134c52e520ed8877e03f78b46 Mon Sep 17 00:00:00 2001 From: Pascal Thormeier Date: Wed, 17 Jun 2015 14:18:32 +0200 Subject: [PATCH] Improve error reporting in case of exception Make Exception messages more verbose --- src/Handlebars/Arguments.php | 5 +++- src/Handlebars/Context.php | 32 +++++++++++++++++--------- src/Handlebars/Handlebars.php | 3 ++- src/Handlebars/Helpers.php | 22 +++++++++++++----- src/Handlebars/Loader/InlineLoader.php | 12 +++++++--- src/Handlebars/Parser.php | 7 +++--- src/Handlebars/Template.php | 14 ++++++----- tests/Xamin/HandlebarsTest.php | 2 +- 8 files changed, 65 insertions(+), 32 deletions(-) diff --git a/src/Handlebars/Arguments.php b/src/Handlebars/Arguments.php index b8ff6b5..6e20db5 100644 --- a/src/Handlebars/Arguments.php +++ b/src/Handlebars/Arguments.php @@ -142,7 +142,10 @@ class Arguments // Remove found argument from arguments string. $current_str = ltrim(substr($current_str, strlen($matches[0]))); } else { - throw new \InvalidArgumentException('Malformed arguments string'); + throw new \InvalidArgumentException(sprintf( + 'Malformed arguments string: "%s"', + $args_string + )); } } } diff --git a/src/Handlebars/Context.php b/src/Handlebars/Context.php index 79a7f70..3b42481 100644 --- a/src/Handlebars/Context.php +++ b/src/Handlebars/Context.php @@ -196,9 +196,10 @@ class Context } if (count($this->stack) < $level) { if ($strict) { - throw new \InvalidArgumentException( - 'can not find variable in context' - ); + throw new \InvalidArgumentException(sprintf( + 'Can not find variable in context: "%s"', + $variableName + )); } return ''; @@ -215,10 +216,12 @@ class Context $current = current($this->stack); if (!$variableName) { if ($strict) { - throw new \InvalidArgumentException( - 'can not find variable in context' - ); + throw new \InvalidArgumentException(sprintf( + 'Can not find variable in context: "%s"', + $variableName + )); } + return ''; } elseif ($variableName == '.' || $variableName == 'this') { return $current; @@ -227,9 +230,10 @@ class Context if (isset($specialVariables[$variableName])) { return $specialVariables[$variableName]; } elseif ($strict) { - throw new \InvalidArgumentException( - 'can not find variable in context' - ); + throw new \InvalidArgumentException(sprintf( + 'Can not find variable in context: "%s"', + $variableName + )); } else { return ''; } @@ -275,7 +279,10 @@ class Context } if ($strict) { - throw new \InvalidArgumentException('can not find variable in context'); + throw new \InvalidArgumentException(sprintf( + 'Can not find variable in context: "%s"', + $variable + )); } return $value; @@ -299,7 +306,10 @@ class Context $get_pattern = "/(?:" . $name_pattern . ")/"; if (!preg_match($check_pattern, $variableName)) { - throw new \InvalidArgumentException('variable name is invalid'); + throw new \InvalidArgumentException(sprintf( + 'Variable name is invalid: "%s"', + $variableName + )); } preg_match_all($get_pattern, $variableName, $matches); diff --git a/src/Handlebars/Handlebars.php b/src/Handlebars/Handlebars.php index 5f4462c..b1816ac 100755 --- a/src/Handlebars/Handlebars.php +++ b/src/Handlebars/Handlebars.php @@ -460,7 +460,8 @@ class Handlebars { if (!is_a($class, 'Handlebars\\Template', true)) { throw new \InvalidArgumentException( - 'Custom template class must extend Template' + 'Custom template class "%s" must extend Template', + $class ); } diff --git a/src/Handlebars/Helpers.php b/src/Handlebars/Helpers.php index 8dfb516..8123071 100644 --- a/src/Handlebars/Helpers.php +++ b/src/Handlebars/Helpers.php @@ -99,9 +99,10 @@ class Helpers public function add($name, $helper) { if (!is_callable($helper) && ! $helper instanceof Helper) { - throw new \InvalidArgumentException( - "$name Helper is not a callable or doesn't implement the Helper interface." - ); + throw new \InvalidArgumentException(sprintf( + "%s Helper is not a callable or doesn't implement the Helper interface.", + $name + )); } $this->helpers[$name] = $helper; } @@ -136,7 +137,10 @@ class Helpers public function call($name, Template $template, Context $context, $args, $source) { if (!$this->has($name)) { - throw new \InvalidArgumentException('Unknown helper: ' . $name); + throw new \InvalidArgumentException(sprintf( + 'Unknown helper: "%s"', + $name + )); } if ($this->helpers[$name] instanceof Helper) { @@ -171,7 +175,10 @@ class Helpers public function __get($name) { if (!$this->has($name)) { - throw new \InvalidArgumentException('Unknown helper :' . $name); + throw new \InvalidArgumentException(sprintf( + 'Unknown helper: "%s"', + $name + )); } return $this->helpers[$name]; @@ -226,7 +233,10 @@ class Helpers public function remove($name) { if (!$this->has($name)) { - throw new \InvalidArgumentException('Unknown helper: ' . $name); + throw new \InvalidArgumentException(sprintf( + 'Unknown helper: "%s"', + $name + )); } unset($this->helpers[$name]); diff --git a/src/Handlebars/Loader/InlineLoader.php b/src/Handlebars/Loader/InlineLoader.php index 5cc2908..62a5bac 100644 --- a/src/Handlebars/Loader/InlineLoader.php +++ b/src/Handlebars/Loader/InlineLoader.php @@ -75,11 +75,17 @@ class InlineLoader implements Loader public function __construct($fileName, $offset) { if (!is_file($fileName)) { - throw new \InvalidArgumentException('InlineLoader expects a valid filename.'); + throw new \InvalidArgumentException(sprintf( + 'InlineLoader expects a valid filename, "%s" given.', + $fileName + )); } if (!is_int($offset) || $offset < 0) { - throw new \InvalidArgumentException('InlineLoader expects a valid file offset.'); + throw new \InvalidArgumentException(sprintf( + 'InlineLoader expects a valid file offset, "%s" given.', + $offset + )); } $this->fileName = $fileName; @@ -98,7 +104,7 @@ class InlineLoader implements Loader $this->loadTemplates(); if (!array_key_exists($name, $this->templates)) { - throw new \InvalidArgumentException("Template {$name} not found."); + throw new \InvalidArgumentException("Template $name not found."); } return $this->templates[$name]; diff --git a/src/Handlebars/Parser.php b/src/Handlebars/Parser.php index 3417bfb..b40f5ee 100755 --- a/src/Handlebars/Parser.php +++ b/src/Handlebars/Parser.php @@ -78,9 +78,10 @@ class Parser do { $result = array_pop($stack); if ($result === null) { - throw new \LogicException( - 'Unexpected closing tag: /' . $token[Tokenizer::NAME] - ); + throw new \LogicException(sprintf( + 'Unexpected closing tag: /%s', + $token[Tokenizer::NAME] + )); } if (!array_key_exists(Tokenizer::NODES, $result) diff --git a/src/Handlebars/Template.php b/src/Handlebars/Template.php index 275e547..4e4b6b7 100644 --- a/src/Handlebars/Template.php +++ b/src/Handlebars/Template.php @@ -404,9 +404,10 @@ class Template try { $sectionVar = $context->get($sectionName, true); } catch (\InvalidArgumentException $e) { - throw new \RuntimeException( - $sectionName . ' is not registered as a helper' - ); + throw new \RuntimeException(sprintf( + '"%s" is not registered as a helper', + $sectionName + )); } $buffer = ''; if (is_array($sectionVar) || $sectionVar instanceof \Traversable) { @@ -461,9 +462,10 @@ class Template } elseif (trim($current[Tokenizer::ARGS]) == '') { return $this->_mustacheStyleSection($context, $current); } else { - throw new \RuntimeException( - $sectionName . ' is not registered as a helper' - ); + throw new \RuntimeException(sprintf( + '"%s"" is not registered as a helper', + $sectionName + )); } } diff --git a/tests/Xamin/HandlebarsTest.php b/tests/Xamin/HandlebarsTest.php index fe5a5c1..ecbbd46 100644 --- a/tests/Xamin/HandlebarsTest.php +++ b/tests/Xamin/HandlebarsTest.php @@ -176,7 +176,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase '{{#if first}}The first{{else}}{{#if second}}The second{{/if}}{{/if}}', array('first' => false, 'second' => true), 'The second' - ) + ), ); }