From 3062b3ef961a0140ed5d3fc7d1bd13ce26c018c6 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Wed, 23 Jul 2014 16:39:28 +0200 Subject: [PATCH] [Config] Allow extra hint in exception message --- .../Component/Config/Definition/ArrayNode.php | 3 +++ .../Config/Definition/BooleanNode.php | 3 +++ .../InvalidConfigurationException.php | 16 ++++++++++++++++ .../Component/Config/Definition/FloatNode.php | 3 +++ .../Config/Definition/IntegerNode.php | 3 +++ .../Config/Definition/ScalarNode.php | 3 +++ .../Config/Definition/VariableNode.php | 3 +++ .../Tests/Definition/ScalarNodeTest.php | 19 +++++++++++++++++++ 8 files changed, 53 insertions(+) diff --git a/src/Symfony/Component/Config/Definition/ArrayNode.php b/src/Symfony/Component/Config/Definition/ArrayNode.php index b83b1048a97f..462c8db61947 100644 --- a/src/Symfony/Component/Config/Definition/ArrayNode.php +++ b/src/Symfony/Component/Config/Definition/ArrayNode.php @@ -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; diff --git a/src/Symfony/Component/Config/Definition/BooleanNode.php b/src/Symfony/Component/Config/Definition/BooleanNode.php index 939e86fa731a..df06b307aea4 100644 --- a/src/Symfony/Component/Config/Definition/BooleanNode.php +++ b/src/Symfony/Component/Config/Definition/BooleanNode.php @@ -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; diff --git a/src/Symfony/Component/Config/Definition/Exception/InvalidConfigurationException.php b/src/Symfony/Component/Config/Definition/Exception/InvalidConfigurationException.php index 840e3f39d924..3dbc57b15317 100644 --- a/src/Symfony/Component/Config/Definition/Exception/InvalidConfigurationException.php +++ b/src/Symfony/Component/Config/Definition/Exception/InvalidConfigurationException.php @@ -20,6 +20,7 @@ class InvalidConfigurationException extends Exception { private $path; + private $containsHints = false; public function setPath($path) { @@ -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; + } + } } diff --git a/src/Symfony/Component/Config/Definition/FloatNode.php b/src/Symfony/Component/Config/Definition/FloatNode.php index 0fe0e34581cb..5e1af17ada07 100644 --- a/src/Symfony/Component/Config/Definition/FloatNode.php +++ b/src/Symfony/Component/Config/Definition/FloatNode.php @@ -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; diff --git a/src/Symfony/Component/Config/Definition/IntegerNode.php b/src/Symfony/Component/Config/Definition/IntegerNode.php index f76efe60c774..ba2307024cae 100644 --- a/src/Symfony/Component/Config/Definition/IntegerNode.php +++ b/src/Symfony/Component/Config/Definition/IntegerNode.php @@ -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; diff --git a/src/Symfony/Component/Config/Definition/ScalarNode.php b/src/Symfony/Component/Config/Definition/ScalarNode.php index 44ccfc56b289..854c265763a7 100644 --- a/src/Symfony/Component/Config/Definition/ScalarNode.php +++ b/src/Symfony/Component/Config/Definition/ScalarNode.php @@ -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; diff --git a/src/Symfony/Component/Config/Definition/VariableNode.php b/src/Symfony/Component/Config/Definition/VariableNode.php index 6d89e49df5d8..b42e1801a114 100644 --- a/src/Symfony/Component/Config/Definition/VariableNode.php +++ b/src/Symfony/Component/Config/Definition/VariableNode.php @@ -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; diff --git a/src/Symfony/Component/Config/Tests/Definition/ScalarNodeTest.php b/src/Symfony/Component/Config/Tests/Definition/ScalarNodeTest.php index 056dd73b0786..a79841071ca8 100644 --- a/src/Symfony/Component/Config/Tests/Definition/ScalarNodeTest.php +++ b/src/Symfony/Component/Config/Tests/Definition/ScalarNodeTest.php @@ -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()); + } }