Skip to content

Commit

Permalink
[Config] Allow extra hint in exception message
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj authored and fabpot committed Sep 25, 2014
1 parent cbfcc77 commit 3062b3e
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Symfony/Component/Config/Definition/ArrayNode.php
Expand Up @@ -269,6 +269,9 @@ protected function validateType($value)
$this->getPath(),
gettype($value)
));
if ($hint = $this->getInfo()) {
$ex->addHint($hint);
}
$ex->setPath($this->getPath());

throw $ex;
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Config/Definition/BooleanNode.php
Expand Up @@ -31,6 +31,9 @@ protected function validateType($value)
$this->getPath(),
gettype($value)
));
if ($hint = $this->getInfo()) {
$ex->addHint($hint);
}
$ex->setPath($this->getPath());

throw $ex;
Expand Down
Expand Up @@ -20,6 +20,7 @@
class InvalidConfigurationException extends Exception
{
private $path;
private $containsHints = false;

public function setPath($path)
{
Expand All @@ -30,4 +31,19 @@ public function getPath()
{
return $this->path;
}

/**
* Adds extra information that is suffixed to the original exception message.
*
* @param string $hint
*/
public function addHint($hint)
{
if (!$this->containsHints) {
$this->message .= "\nHint: ".$hint;
$this->containsHints = true;
} else {
$this->message .= ', '.$hint;
}
}
}
3 changes: 3 additions & 0 deletions src/Symfony/Component/Config/Definition/FloatNode.php
Expand Up @@ -32,6 +32,9 @@ protected function validateType($value)

if (!is_float($value)) {
$ex = new InvalidTypeException(sprintf('Invalid type for path "%s". Expected float, but got %s.', $this->getPath(), gettype($value)));
if ($hint = $this->getInfo()) {
$ex->addHint($hint);
}
$ex->setPath($this->getPath());

throw $ex;
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Config/Definition/IntegerNode.php
Expand Up @@ -27,6 +27,9 @@ protected function validateType($value)
{
if (!is_int($value)) {
$ex = new InvalidTypeException(sprintf('Invalid type for path "%s". Expected int, but got %s.', $this->getPath(), gettype($value)));
if ($hint = $this->getInfo()) {
$ex->addHint($hint);
}
$ex->setPath($this->getPath());

throw $ex;
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Config/Definition/ScalarNode.php
Expand Up @@ -38,6 +38,9 @@ protected function validateType($value)
$this->getPath(),
gettype($value)
));
if ($hint = $this->getInfo()) {
$ex->addHint($hint);
}
$ex->setPath($this->getPath());

throw $ex;
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Config/Definition/VariableNode.php
Expand Up @@ -88,6 +88,9 @@ protected function finalizeValue($value)
$this->getPath(),
json_encode($value)
));
if ($hint = $this->getInfo()) {
$ex->addHint($hint);
}
$ex->setPath($this->getPath());

throw $ex;
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/Config/Tests/Definition/ScalarNodeTest.php
Expand Up @@ -57,4 +57,23 @@ public function getInvalidValues()
array(new \stdClass()),
);
}

public function testNormalizeThrowsExceptionWithoutHint()
{
$node = new ScalarNode('test');

$this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', 'Invalid type for path "test". Expected scalar, but got array.');

$node->normalize(array());
}

public function testNormalizeThrowsExceptionWithErrorMessage()
{
$node = new ScalarNode('test');
$node->setInfo('"the test value"');

$this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', "Invalid type for path \"test\". Expected scalar, but got array.\nHint: \"the test value\"");

$node->normalize(array());
}
}

0 comments on commit 3062b3e

Please sign in to comment.