From 6d5b4e5a379d98c2b901165f04b7e3072ac1a6eb Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Sun, 28 May 2017 11:44:49 +0200 Subject: [PATCH] Use dedicated exception for scalar type parsing error --- src/Error/UserError.php | 14 ++++++++++++++ src/Type/Definition/FloatType.php | 6 +++--- src/Type/Definition/IntType.php | 8 ++++---- src/Type/Definition/ObjectType.php | 5 +++-- tests/Type/ScalarSerializationTest.php | 18 +++++++++--------- 5 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 src/Error/UserError.php diff --git a/src/Error/UserError.php b/src/Error/UserError.php new file mode 100644 index 0000000..ad07ee9 --- /dev/null +++ b/src/Error/UserError.php @@ -0,0 +1,14 @@ += self::MIN_INT) { return (int) $value; } - throw new InvariantViolation( - 'Int cannot represent non 32-bit signed integer value: ' . Utils::printSafe($value) + throw new UserError( + sprintf('Int cannot represent non 32-bit signed integer value: %s', Utils::printSafe($value)) ); } diff --git a/src/Type/Definition/ObjectType.php b/src/Type/Definition/ObjectType.php index 4bcd153..7a2fd65 100644 --- a/src/Type/Definition/ObjectType.php +++ b/src/Type/Definition/ObjectType.php @@ -1,6 +1,7 @@ getFields(); } if (!isset($this->fields[$name])) { - throw new InvariantViolation(sprintf("Field '%s' is not defined for type '%s'", $name, $this->name)); + throw new UserError(sprintf("Field '%s' is not defined for type '%s'", $name, $this->name)); } return $this->fields[$name]; } @@ -145,7 +146,7 @@ class ObjectType extends Type implements OutputType, CompositeType foreach ($interfaces as $iface) { $iface = Type::resolve($iface); if (!$iface instanceof InterfaceType) { - throw new InvariantViolation("Expecting interface type, got " . Utils::printSafe($iface)); + throw new InvariantViolation(sprintf('Expecting interface type, got %s', Utils::printSafe($iface))); } $this->interfaces[] = $iface; } diff --git a/tests/Type/ScalarSerializationTest.php b/tests/Type/ScalarSerializationTest.php index e0be2fa..b097a10 100644 --- a/tests/Type/ScalarSerializationTest.php +++ b/tests/Type/ScalarSerializationTest.php @@ -1,7 +1,7 @@ serialize(9876504321); $this->fail('Expected exception was not thrown'); - } catch (InvariantViolation $e) { + } catch (UserError $e) { $this->assertEquals('Int cannot represent non 32-bit signed integer value: 9876504321', $e->getMessage()); } try { $intType->serialize(-9876504321); $this->fail('Expected exception was not thrown'); - } catch (InvariantViolation $e) { + } catch (UserError $e) { $this->assertEquals('Int cannot represent non 32-bit signed integer value: -9876504321', $e->getMessage()); } try { $intType->serialize(1e100); $this->fail('Expected exception was not thrown'); - } catch (InvariantViolation $e) { + } catch (UserError $e) { $this->assertEquals('Int cannot represent non 32-bit signed integer value: 1.0E+100', $e->getMessage()); } try { $intType->serialize(-1e100); $this->fail('Expected exception was not thrown'); - } catch (InvariantViolation $e) { + } catch (UserError $e) { $this->assertEquals('Int cannot represent non 32-bit signed integer value: -1.0E+100', $e->getMessage()); } @@ -58,14 +58,14 @@ class ScalarSerializationTest extends \PHPUnit_Framework_TestCase try { $intType->serialize('one'); $this->fail('Expected exception was not thrown'); - } catch (InvariantViolation $e) { + } catch (UserError $e) { $this->assertEquals('Int cannot represent non 32-bit signed integer value: one', $e->getMessage()); } try { $intType->serialize(''); $this->fail('Expected exception was not thrown'); - } catch (InvariantViolation $e) { + } catch (UserError $e) { $this->assertEquals('Int cannot represent non 32-bit signed integer value: (empty string)', $e->getMessage()); } @@ -92,14 +92,14 @@ class ScalarSerializationTest extends \PHPUnit_Framework_TestCase try { $floatType->serialize('one'); $this->fail('Expected exception was not thrown'); - } catch (InvariantViolation $e) { + } catch (UserError $e) { $this->assertEquals('Float cannot represent non numeric value: one', $e->getMessage()); } try { $floatType->serialize(''); $this->fail('Expected exception was not thrown'); - } catch (InvariantViolation $e) { + } catch (UserError $e) { $this->assertEquals('Float cannot represent non numeric value: (empty string)', $e->getMessage()); }