[Config] some exception improvements
This commit is contained in:
parent
4fd5585e03
commit
486ecdc6a6
@ -11,6 +11,8 @@
|
||||
|
||||
namespace Symfony\Component\Config\Definition;
|
||||
|
||||
use Symfony\Component\Config\Definition\Exception\ForbiddenOverwriteException;
|
||||
|
||||
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
|
||||
use Symfony\Component\Config\Definition\Exception\DuplicateKeyException;
|
||||
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
|
||||
@ -193,8 +195,11 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
|
||||
foreach ($this->children as $name => $child) {
|
||||
if (!array_key_exists($name, $value)) {
|
||||
if ($child->isRequired()) {
|
||||
$msg = sprintf('The node at path "%s.%s" must be configured.', $this->getPath(), $name);
|
||||
throw new InvalidConfigurationException($msg);
|
||||
$msg = sprintf('The child node "%s" at path "%s" must be configured.', $name, $this->getPath());
|
||||
$ex = new InvalidConfigurationException($msg);
|
||||
$ex->setPath($this->getPath());
|
||||
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
if ($child->hasDefaultValue()) {
|
||||
@ -223,11 +228,14 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
|
||||
protected function validateType($value)
|
||||
{
|
||||
if (!is_array($value) && (!$this->allowFalse || false !== $value)) {
|
||||
throw new InvalidTypeException(sprintf(
|
||||
$ex = new InvalidTypeException(sprintf(
|
||||
'Invalid type for path "%s". Expected array, but got %s',
|
||||
$this->getPath(),
|
||||
gettype($value)
|
||||
));
|
||||
$ex->setPath($this->getPath());
|
||||
|
||||
throw $ex;
|
||||
}
|
||||
}
|
||||
|
||||
@ -256,7 +264,10 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
|
||||
// if extra fields are present, throw exception
|
||||
if (count($value) && !$this->ignoreExtraKeys) {
|
||||
$msg = sprintf('Unrecognized options "%s" under "%s"', implode(', ', array_keys($value)), $this->getPath());
|
||||
throw new InvalidConfigurationException($msg);
|
||||
$ex = new InvalidConfigurationException($msg);
|
||||
$ex->setPath($this->getPath().'.'.reset($value));
|
||||
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
@ -309,13 +320,16 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
|
||||
// no conflict
|
||||
if (!array_key_exists($k, $leftSide)) {
|
||||
if (!$this->allowNewKeys) {
|
||||
throw new InvalidConfigurationException(sprintf(
|
||||
$ex = new InvalidConfigurationException(sprintf(
|
||||
'You are not allowed to define new elements for path "%s". '
|
||||
.'Please define all elements for this path in one config file. '
|
||||
.'If you are trying to overwrite an element, make sure you redefine it '
|
||||
.'with the same name.',
|
||||
$this->getPath()
|
||||
));
|
||||
$ex->setPath($this->getPath());
|
||||
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
$leftSide[$k] = $v;
|
||||
|
@ -26,11 +26,14 @@ class BooleanNode extends ScalarNode
|
||||
protected function validateType($value)
|
||||
{
|
||||
if (!is_bool($value)) {
|
||||
throw new InvalidTypeException(sprintf(
|
||||
$ex = new InvalidTypeException(sprintf(
|
||||
'Invalid type for path "%s". Expected boolean, but got %s.',
|
||||
$this->getPath(),
|
||||
gettype($value)
|
||||
));
|
||||
$ex->setPath($this->getPath());
|
||||
|
||||
throw $ex;
|
||||
}
|
||||
}
|
||||
}
|
@ -19,4 +19,15 @@ namespace Symfony\Component\Config\Definition\Exception;
|
||||
*/
|
||||
class InvalidConfigurationException extends Exception
|
||||
{
|
||||
private $path;
|
||||
|
||||
public function setPath($path)
|
||||
{
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
public function getPath()
|
||||
{
|
||||
return $this->path;
|
||||
}
|
||||
}
|
@ -161,7 +161,10 @@ class PrototypedArrayNode extends ArrayNode
|
||||
|
||||
if (count($value) < $this->minNumberOfElements) {
|
||||
$msg = sprintf('The path "%s" should have at least %d element(s) defined.', $this->getPath(), $this->minNumberOfElements);
|
||||
throw new InvalidConfigurationException($msg);
|
||||
$ex = new InvalidConfigurationException($msg);
|
||||
$ex->setPath($this->getPath());
|
||||
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
return $value;
|
||||
@ -186,7 +189,10 @@ class PrototypedArrayNode extends ArrayNode
|
||||
if (null !== $this->keyAttribute && is_array($v)) {
|
||||
if (!isset($v[$this->keyAttribute]) && is_int($k)) {
|
||||
$msg = sprintf('The attribute "%s" must be set for path "%s".', $this->keyAttribute, $this->getPath());
|
||||
throw new InvalidConfigurationException($msg);
|
||||
$ex = new InvalidConfigurationException($msg);
|
||||
$ex->setPath($this->getPath());
|
||||
|
||||
throw $ex;
|
||||
} else if (isset($v[$this->keyAttribute])) {
|
||||
$k = $v[$this->keyAttribute];
|
||||
|
||||
@ -203,7 +209,10 @@ class PrototypedArrayNode extends ArrayNode
|
||||
|
||||
if (array_key_exists($k, $normalized)) {
|
||||
$msg = sprintf('Duplicate key "%s" for path "%s".', $k, $this->getPath());
|
||||
throw new DuplicateKeyException($msg);
|
||||
$ex = new DuplicateKeyException($msg);
|
||||
$ex->setPath($this->getPath());
|
||||
|
||||
throw $ex;
|
||||
}
|
||||
}
|
||||
|
||||
@ -249,11 +258,14 @@ class PrototypedArrayNode extends ArrayNode
|
||||
// no conflict
|
||||
if (!array_key_exists($k, $leftSide)) {
|
||||
if (!$this->allowNewKeys) {
|
||||
throw new InvalidConfigurationException(sprintf(
|
||||
$ex = new InvalidConfigurationException(sprintf(
|
||||
'You are not allowed to define new elements for path "%s". ' .
|
||||
'Please define all elements for this path in one config file.',
|
||||
$this->getPath()
|
||||
));
|
||||
$ex->setPath($this->getPath());
|
||||
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
$leftSide[$k] = $v;
|
||||
|
@ -34,11 +34,14 @@ class ScalarNode extends VariableNode
|
||||
protected function validateType($value)
|
||||
{
|
||||
if (!is_scalar($value) && null !== $value) {
|
||||
throw new InvalidTypeException(sprintf(
|
||||
$ex = new InvalidTypeException(sprintf(
|
||||
'Invalid type for path "%s". Expected scalar, but got %s.',
|
||||
$this->getPath(),
|
||||
gettype($value)
|
||||
));
|
||||
$ex->setPath($this->getPath());
|
||||
|
||||
throw $ex;
|
||||
}
|
||||
}
|
||||
}
|
@ -84,11 +84,14 @@ class VariableNode extends BaseNode implements PrototypeNodeInterface
|
||||
protected function finalizeValue($value)
|
||||
{
|
||||
if (!$this->allowEmptyValue && empty($value)) {
|
||||
throw new InvalidConfigurationException(sprintf(
|
||||
$ex = new InvalidConfigurationException(sprintf(
|
||||
'The path "%s" cannot contain an empty value, but got %s.',
|
||||
$this->getPath(),
|
||||
json_encode($value)
|
||||
));
|
||||
$ex->setPath($this->getPath());
|
||||
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
return $value;
|
||||
|
Reference in New Issue
Block a user