From 50152f1b29d99247c2494a69f7f7071d5a9d37f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Pel=C3=AD=C5=A1ek?= Date: Sat, 9 Jan 2021 19:38:55 +0100 Subject: [PATCH] Improved interface contract error messages --- .../ArgumentConstraintNotContravariant.php | 9 ++++- .../Type/FieldConstraintNotCovariant.php | 9 ++++- .../InterfaceContractArgumentTypeMismatch.php | 9 ++++- .../InterfaceContractFieldTypeMismatch.php | 9 ++++- .../Type/InterfaceContractMissingArgument.php | 9 ++++- .../Type/InterfaceContractMissingField.php | 9 ++++- src/Type/Contract/TInterfaceImplementor.php | 39 ++++++++++++++++--- 7 files changed, 81 insertions(+), 12 deletions(-) diff --git a/src/Exception/Type/ArgumentConstraintNotContravariant.php b/src/Exception/Type/ArgumentConstraintNotContravariant.php index 25a5005c0..bc49d0648 100644 --- a/src/Exception/Type/ArgumentConstraintNotContravariant.php +++ b/src/Exception/Type/ArgumentConstraintNotContravariant.php @@ -6,5 +6,12 @@ final class ArgumentConstraintNotContravariant extends \Graphpinator\Exception\Type\TypeError { - public const MESSAGE = 'Argument constraint is not contravariant.'; + public const MESSAGE = 'Type "%s" does not satisfy interface "%s" - argument "%s" on field "%s" has constraint which is not contravariant.'; + + public function __construct(string $childName, string $interfaceName, string $fieldName, string $argumentName) + { + $this->messageArgs = [$childName, $interfaceName, $argumentName, $fieldName]; + + parent::__construct(); + } } diff --git a/src/Exception/Type/FieldConstraintNotCovariant.php b/src/Exception/Type/FieldConstraintNotCovariant.php index 2d1428c9a..43d3323a2 100644 --- a/src/Exception/Type/FieldConstraintNotCovariant.php +++ b/src/Exception/Type/FieldConstraintNotCovariant.php @@ -6,5 +6,12 @@ final class FieldConstraintNotCovariant extends \Graphpinator\Exception\Type\TypeError { - public const MESSAGE = 'Field constraint is not covariant.'; + public const MESSAGE = 'Type "%s" does not satisfy interface "%s" - field "%s" has constraint which is not covariant.'; + + public function __construct(string $childName, string $interfaceName, string $fieldName) + { + $this->messageArgs = [$childName, $interfaceName, $fieldName]; + + parent::__construct(); + } } diff --git a/src/Exception/Type/InterfaceContractArgumentTypeMismatch.php b/src/Exception/Type/InterfaceContractArgumentTypeMismatch.php index 3527fde95..472423773 100644 --- a/src/Exception/Type/InterfaceContractArgumentTypeMismatch.php +++ b/src/Exception/Type/InterfaceContractArgumentTypeMismatch.php @@ -6,5 +6,12 @@ final class InterfaceContractArgumentTypeMismatch extends \Graphpinator\Exception\Type\TypeError { - public const MESSAGE = 'Type doesnt satisfy interface - argument type does not match'; + public const MESSAGE = 'Type "%s" does not satisfy interface "%s" - argument "%s" on field "%s" does not have a compatible type.'; + + public function __construct(string $childName, string $interfaceName, string $fieldName, string $argumentName) + { + $this->messageArgs = [$childName, $interfaceName, $argumentName, $fieldName]; + + parent::__construct(); + } } diff --git a/src/Exception/Type/InterfaceContractFieldTypeMismatch.php b/src/Exception/Type/InterfaceContractFieldTypeMismatch.php index 2cc74b58f..cabf3bdc2 100644 --- a/src/Exception/Type/InterfaceContractFieldTypeMismatch.php +++ b/src/Exception/Type/InterfaceContractFieldTypeMismatch.php @@ -6,5 +6,12 @@ final class InterfaceContractFieldTypeMismatch extends \Graphpinator\Exception\Type\TypeError { - public const MESSAGE = 'Type doesnt satisfy interface - field type does not match'; + public const MESSAGE = 'Type "%s" does not satisfy interface "%s" - field "%s" does not have a compatible type.'; + + public function __construct(string $childName, string $interfaceName, string $fieldName) + { + $this->messageArgs = [$childName, $interfaceName, $fieldName]; + + parent::__construct(); + } } diff --git a/src/Exception/Type/InterfaceContractMissingArgument.php b/src/Exception/Type/InterfaceContractMissingArgument.php index 945d84c52..272704061 100644 --- a/src/Exception/Type/InterfaceContractMissingArgument.php +++ b/src/Exception/Type/InterfaceContractMissingArgument.php @@ -6,5 +6,12 @@ final class InterfaceContractMissingArgument extends \Graphpinator\Exception\Type\TypeError { - public const MESSAGE = 'Type doesnt satisfy interface - field has missing argument'; + public const MESSAGE = 'Type "%s" does not satisfy interface "%s" - argument "%s" on field "%s" is missing.'; + + public function __construct(string $childName, string $interfaceName, string $fieldName, string $argumentName) + { + $this->messageArgs = [$childName, $interfaceName, $argumentName, $fieldName]; + + parent::__construct(); + } } diff --git a/src/Exception/Type/InterfaceContractMissingField.php b/src/Exception/Type/InterfaceContractMissingField.php index 16d929f4f..7512ed561 100644 --- a/src/Exception/Type/InterfaceContractMissingField.php +++ b/src/Exception/Type/InterfaceContractMissingField.php @@ -6,5 +6,12 @@ final class InterfaceContractMissingField extends \Graphpinator\Exception\Type\TypeError { - public const MESSAGE = 'Type doesnt satisfy interface - missing field'; + public const MESSAGE = 'Type "%s" does not satisfy interface "%s" - missing field "%s".'; + + public function __construct(string $childName, string $interfaceName, string $fieldName) + { + $this->messageArgs = [$childName, $interfaceName, $fieldName]; + + parent::__construct(); + } } diff --git a/src/Type/Contract/TInterfaceImplementor.php b/src/Type/Contract/TInterfaceImplementor.php index 48d75a6ea..16de73e9e 100644 --- a/src/Type/Contract/TInterfaceImplementor.php +++ b/src/Type/Contract/TInterfaceImplementor.php @@ -54,32 +54,59 @@ protected function validateInterfaces() : void foreach ($interface->getFields() as $fieldContract) { if (!$this->getFields()->offsetExists($fieldContract->getName())) { - throw new \Graphpinator\Exception\Type\InterfaceContractMissingField(); + throw new \Graphpinator\Exception\Type\InterfaceContractMissingField( + $this->getName(), + $interface->getName(), + $fieldContract->getName(), + ); } $field = $this->getFields()->offsetGet($fieldContract->getName()); if (!$fieldContract->getType()->isInstanceOf($field->getType())) { - throw new \Graphpinator\Exception\Type\InterfaceContractFieldTypeMismatch(); + throw new \Graphpinator\Exception\Type\InterfaceContractFieldTypeMismatch( + $this->getName(), + $interface->getName(), + $fieldContract->getName(), + ); } if (!$fieldContract->getConstraints()->isCovariant($field->getConstraints())) { - throw new \Graphpinator\Exception\Type\FieldConstraintNotCovariant(); + throw new \Graphpinator\Exception\Type\FieldConstraintNotCovariant( + $this->getName(), + $interface->getName(), + $fieldContract->getName(), + ); } foreach ($fieldContract->getArguments() as $argumentContract) { if (!$field->getArguments()->offsetExists($argumentContract->getName())) { - throw new \Graphpinator\Exception\Type\InterfaceContractMissingArgument(); + throw new \Graphpinator\Exception\Type\InterfaceContractMissingArgument( + $this->getName(), + $interface->getName(), + $fieldContract->getName(), + $argumentContract->getName(), + ); } $argument = $field->getArguments()->offsetGet($argumentContract->getName()); if (!$argument->getType()->isInstanceOf($argumentContract->getType())) { - throw new \Graphpinator\Exception\Type\InterfaceContractArgumentTypeMismatch(); + throw new \Graphpinator\Exception\Type\InterfaceContractArgumentTypeMismatch( + $this->getName(), + $interface->getName(), + $fieldContract->getName(), + $argumentContract->getName(), + ); } if (!$argumentContract->getConstraints()->isContravariant($argument->getConstraints())) { - throw new \Graphpinator\Exception\Type\ArgumentConstraintNotContravariant(); + throw new \Graphpinator\Exception\Type\ArgumentConstraintNotContravariant( + $this->getName(), + $interface->getName(), + $fieldContract->getName(), + $argumentContract->getName(), + ); } } }