Skip to content

Commit 3062b3e

Browse files
wouterjfabpot
authored andcommitted
[Config] Allow extra hint in exception message
1 parent cbfcc77 commit 3062b3e

File tree

8 files changed

+53
-0
lines changed

8 files changed

+53
-0
lines changed

src/Symfony/Component/Config/Definition/ArrayNode.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ protected function validateType($value)
269269
$this->getPath(),
270270
gettype($value)
271271
));
272+
if ($hint = $this->getInfo()) {
273+
$ex->addHint($hint);
274+
}
272275
$ex->setPath($this->getPath());
273276

274277
throw $ex;

src/Symfony/Component/Config/Definition/BooleanNode.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ protected function validateType($value)
3131
$this->getPath(),
3232
gettype($value)
3333
));
34+
if ($hint = $this->getInfo()) {
35+
$ex->addHint($hint);
36+
}
3437
$ex->setPath($this->getPath());
3538

3639
throw $ex;

src/Symfony/Component/Config/Definition/Exception/InvalidConfigurationException.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
class InvalidConfigurationException extends Exception
2121
{
2222
private $path;
23+
private $containsHints = false;
2324

2425
public function setPath($path)
2526
{
@@ -30,4 +31,19 @@ public function getPath()
3031
{
3132
return $this->path;
3233
}
34+
35+
/**
36+
* Adds extra information that is suffixed to the original exception message.
37+
*
38+
* @param string $hint
39+
*/
40+
public function addHint($hint)
41+
{
42+
if (!$this->containsHints) {
43+
$this->message .= "\nHint: ".$hint;
44+
$this->containsHints = true;
45+
} else {
46+
$this->message .= ', '.$hint;
47+
}
48+
}
3349
}

src/Symfony/Component/Config/Definition/FloatNode.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ protected function validateType($value)
3232

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

3740
throw $ex;

src/Symfony/Component/Config/Definition/IntegerNode.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ protected function validateType($value)
2727
{
2828
if (!is_int($value)) {
2929
$ex = new InvalidTypeException(sprintf('Invalid type for path "%s". Expected int, but got %s.', $this->getPath(), gettype($value)));
30+
if ($hint = $this->getInfo()) {
31+
$ex->addHint($hint);
32+
}
3033
$ex->setPath($this->getPath());
3134

3235
throw $ex;

src/Symfony/Component/Config/Definition/ScalarNode.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ protected function validateType($value)
3838
$this->getPath(),
3939
gettype($value)
4040
));
41+
if ($hint = $this->getInfo()) {
42+
$ex->addHint($hint);
43+
}
4144
$ex->setPath($this->getPath());
4245

4346
throw $ex;

src/Symfony/Component/Config/Definition/VariableNode.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ protected function finalizeValue($value)
8888
$this->getPath(),
8989
json_encode($value)
9090
));
91+
if ($hint = $this->getInfo()) {
92+
$ex->addHint($hint);
93+
}
9194
$ex->setPath($this->getPath());
9295

9396
throw $ex;

src/Symfony/Component/Config/Tests/Definition/ScalarNodeTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,23 @@ public function getInvalidValues()
5757
array(new \stdClass()),
5858
);
5959
}
60+
61+
public function testNormalizeThrowsExceptionWithoutHint()
62+
{
63+
$node = new ScalarNode('test');
64+
65+
$this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', 'Invalid type for path "test". Expected scalar, but got array.');
66+
67+
$node->normalize(array());
68+
}
69+
70+
public function testNormalizeThrowsExceptionWithErrorMessage()
71+
{
72+
$node = new ScalarNode('test');
73+
$node->setInfo('"the test value"');
74+
75+
$this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', "Invalid type for path \"test\". Expected scalar, but got array.\nHint: \"the test value\"");
76+
77+
$node->normalize(array());
78+
}
6079
}

0 commit comments

Comments
 (0)