Improve error reporting in case of exception

Make Exception messages more verbose
This commit is contained in:
Pascal Thormeier 2015-06-17 14:18:32 +02:00
parent 959834be09
commit 0a908fb6a1
8 changed files with 65 additions and 32 deletions

View File

@ -142,7 +142,10 @@ class Arguments
// Remove found argument from arguments string. // Remove found argument from arguments string.
$current_str = ltrim(substr($current_str, strlen($matches[0]))); $current_str = ltrim(substr($current_str, strlen($matches[0])));
} else { } else {
throw new \InvalidArgumentException('Malformed arguments string'); throw new \InvalidArgumentException(sprintf(
'Malformed arguments string: "%s"',
$args_string
));
} }
} }
} }

View File

@ -196,9 +196,10 @@ class Context
} }
if (count($this->stack) < $level) { if (count($this->stack) < $level) {
if ($strict) { if ($strict) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(sprintf(
'can not find variable in context' 'Can not find variable in context: "%s"',
); $variableName
));
} }
return ''; return '';
@ -215,10 +216,12 @@ class Context
$current = current($this->stack); $current = current($this->stack);
if (!$variableName) { if (!$variableName) {
if ($strict) { if ($strict) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(sprintf(
'can not find variable in context' 'Can not find variable in context: "%s"',
); $variableName
));
} }
return ''; return '';
} elseif ($variableName == '.' || $variableName == 'this') { } elseif ($variableName == '.' || $variableName == 'this') {
return $current; return $current;
@ -227,9 +230,10 @@ class Context
if (isset($specialVariables[$variableName])) { if (isset($specialVariables[$variableName])) {
return $specialVariables[$variableName]; return $specialVariables[$variableName];
} elseif ($strict) { } elseif ($strict) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(sprintf(
'can not find variable in context' 'Can not find variable in context: "%s"',
); $variableName
));
} else { } else {
return ''; return '';
} }
@ -275,7 +279,10 @@ class Context
} }
if ($strict) { 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; return $value;
@ -299,7 +306,10 @@ class Context
$get_pattern = "/(?:" . $name_pattern . ")/"; $get_pattern = "/(?:" . $name_pattern . ")/";
if (!preg_match($check_pattern, $variableName)) { 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); preg_match_all($get_pattern, $variableName, $matches);

View File

@ -460,7 +460,8 @@ class Handlebars
{ {
if (!is_a($class, 'Handlebars\\Template', true)) { if (!is_a($class, 'Handlebars\\Template', true)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'Custom template class must extend Template' 'Custom template class "%s" must extend Template',
$class
); );
} }

View File

@ -99,9 +99,10 @@ class Helpers
public function add($name, $helper) public function add($name, $helper)
{ {
if (!is_callable($helper) && ! $helper instanceof Helper) { if (!is_callable($helper) && ! $helper instanceof Helper) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(sprintf(
"$name Helper is not a callable or doesn't implement the Helper interface." "%s Helper is not a callable or doesn't implement the Helper interface.",
); $name
));
} }
$this->helpers[$name] = $helper; $this->helpers[$name] = $helper;
} }
@ -136,7 +137,10 @@ class Helpers
public function call($name, Template $template, Context $context, $args, $source) public function call($name, Template $template, Context $context, $args, $source)
{ {
if (!$this->has($name)) { if (!$this->has($name)) {
throw new \InvalidArgumentException('Unknown helper: ' . $name); throw new \InvalidArgumentException(sprintf(
'Unknown helper: "%s"',
$name
));
} }
if ($this->helpers[$name] instanceof Helper) { if ($this->helpers[$name] instanceof Helper) {
@ -171,7 +175,10 @@ class Helpers
public function __get($name) public function __get($name)
{ {
if (!$this->has($name)) { if (!$this->has($name)) {
throw new \InvalidArgumentException('Unknown helper :' . $name); throw new \InvalidArgumentException(sprintf(
'Unknown helper: "%s"',
$name
));
} }
return $this->helpers[$name]; return $this->helpers[$name];
@ -226,7 +233,10 @@ class Helpers
public function remove($name) public function remove($name)
{ {
if (!$this->has($name)) { if (!$this->has($name)) {
throw new \InvalidArgumentException('Unknown helper: ' . $name); throw new \InvalidArgumentException(sprintf(
'Unknown helper: "%s"',
$name
));
} }
unset($this->helpers[$name]); unset($this->helpers[$name]);

View File

@ -75,11 +75,17 @@ class InlineLoader implements Loader
public function __construct($fileName, $offset) public function __construct($fileName, $offset)
{ {
if (!is_file($fileName)) { 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) { 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; $this->fileName = $fileName;
@ -98,7 +104,7 @@ class InlineLoader implements Loader
$this->loadTemplates(); $this->loadTemplates();
if (!array_key_exists($name, $this->templates)) { 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]; return $this->templates[$name];

View File

@ -78,9 +78,10 @@ class Parser
do { do {
$result = array_pop($stack); $result = array_pop($stack);
if ($result === null) { if ($result === null) {
throw new \LogicException( throw new \LogicException(sprintf(
'Unexpected closing tag: /' . $token[Tokenizer::NAME] 'Unexpected closing tag: /%s',
); $token[Tokenizer::NAME]
));
} }
if (!array_key_exists(Tokenizer::NODES, $result) if (!array_key_exists(Tokenizer::NODES, $result)

View File

@ -404,9 +404,10 @@ class Template
try { try {
$sectionVar = $context->get($sectionName, true); $sectionVar = $context->get($sectionName, true);
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
throw new \RuntimeException( throw new \RuntimeException(sprintf(
$sectionName . ' is not registered as a helper' '"%s" is not registered as a helper',
); $sectionName
));
} }
$buffer = ''; $buffer = '';
if (is_array($sectionVar) || $sectionVar instanceof \Traversable) { if (is_array($sectionVar) || $sectionVar instanceof \Traversable) {
@ -461,9 +462,10 @@ class Template
} elseif (trim($current[Tokenizer::ARGS]) == '') { } elseif (trim($current[Tokenizer::ARGS]) == '') {
return $this->_mustacheStyleSection($context, $current); return $this->_mustacheStyleSection($context, $current);
} else { } else {
throw new \RuntimeException( throw new \RuntimeException(sprintf(
$sectionName . ' is not registered as a helper' '"%s"" is not registered as a helper',
); $sectionName
));
} }
} }

View File

@ -176,7 +176,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
'{{#if first}}The first{{else}}{{#if second}}The second{{/if}}{{/if}}', '{{#if first}}The first{{else}}{{#if second}}The second{{/if}}{{/if}}',
array('first' => false, 'second' => true), array('first' => false, 'second' => true),
'The second' 'The second'
) ),
); );
} }