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.
$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
));
}
}
}

View File

@ -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);

View File

@ -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
);
}

View File

@ -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]);

View File

@ -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];

View File

@ -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)

View File

@ -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
));
}
}

View File

@ -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'
)
),
);
}