mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-29 00:25:17 +03:00
Use dedicated exception for scalar type parsing error
This commit is contained in:
parent
53edfa0f84
commit
6d5b4e5a37
14
src/Error/UserError.php
Normal file
14
src/Error/UserError.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
namespace GraphQL\Error;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class UserError
|
||||||
|
*
|
||||||
|
* Note:
|
||||||
|
* Error that can be display safely to client...
|
||||||
|
*
|
||||||
|
* @package GraphQL\Error
|
||||||
|
*/
|
||||||
|
class UserError extends InvariantViolation
|
||||||
|
{
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace GraphQL\Type\Definition;
|
namespace GraphQL\Type\Definition;
|
||||||
|
|
||||||
use GraphQL\Error\InvariantViolation;
|
use GraphQL\Error\UserError;
|
||||||
use GraphQL\Language\AST\FloatValueNode;
|
use GraphQL\Language\AST\FloatValueNode;
|
||||||
use GraphQL\Language\AST\IntValueNode;
|
use GraphQL\Language\AST\IntValueNode;
|
||||||
use GraphQL\Utils;
|
use GraphQL\Utils;
|
||||||
@ -50,14 +50,14 @@ values as specified by
|
|||||||
private function coerceFloat($value)
|
private function coerceFloat($value)
|
||||||
{
|
{
|
||||||
if ($value === '') {
|
if ($value === '') {
|
||||||
throw new InvariantViolation(
|
throw new UserError(
|
||||||
'Float cannot represent non numeric value: (empty string)'
|
'Float cannot represent non numeric value: (empty string)'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (is_numeric($value) || $value === true || $value === false) {
|
if (is_numeric($value) || $value === true || $value === false) {
|
||||||
return (float)$value;
|
return (float)$value;
|
||||||
}
|
}
|
||||||
throw new InvariantViolation('Float cannot represent non numeric value: ' . Utils::printSafe($value));
|
throw new UserError(sprintf('Float cannot represent non numeric value: %s', Utils::printSafe($value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace GraphQL\Type\Definition;
|
namespace GraphQL\Type\Definition;
|
||||||
|
|
||||||
use GraphQL\Error\InvariantViolation;
|
use GraphQL\Error\UserError;
|
||||||
use GraphQL\Language\AST\IntValueNode;
|
use GraphQL\Language\AST\IntValueNode;
|
||||||
use GraphQL\Language\AST\ValueNode;
|
use GraphQL\Language\AST\ValueNode;
|
||||||
use GraphQL\Utils;
|
use GraphQL\Utils;
|
||||||
@ -57,7 +57,7 @@ values. Int can represent values between -(2^31) and 2^31 - 1. ';
|
|||||||
private function coerceInt($value)
|
private function coerceInt($value)
|
||||||
{
|
{
|
||||||
if ($value === '') {
|
if ($value === '') {
|
||||||
throw new InvariantViolation(
|
throw new UserError(
|
||||||
'Int cannot represent non 32-bit signed integer value: (empty string)'
|
'Int cannot represent non 32-bit signed integer value: (empty string)'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -67,8 +67,8 @@ values. Int can represent values between -(2^31) and 2^31 - 1. ';
|
|||||||
if (is_numeric($value) && $value <= self::MAX_INT && $value >= self::MIN_INT) {
|
if (is_numeric($value) && $value <= self::MAX_INT && $value >= self::MIN_INT) {
|
||||||
return (int) $value;
|
return (int) $value;
|
||||||
}
|
}
|
||||||
throw new InvariantViolation(
|
throw new UserError(
|
||||||
'Int cannot represent non 32-bit signed integer value: ' . Utils::printSafe($value)
|
sprintf('Int cannot represent non 32-bit signed integer value: %s', Utils::printSafe($value))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace GraphQL\Type\Definition;
|
namespace GraphQL\Type\Definition;
|
||||||
use GraphQL\Error\InvariantViolation;
|
use GraphQL\Error\InvariantViolation;
|
||||||
|
use GraphQL\Error\UserError;
|
||||||
use GraphQL\Utils;
|
use GraphQL\Utils;
|
||||||
|
|
||||||
|
|
||||||
@ -127,7 +128,7 @@ class ObjectType extends Type implements OutputType, CompositeType
|
|||||||
$this->getFields();
|
$this->getFields();
|
||||||
}
|
}
|
||||||
if (!isset($this->fields[$name])) {
|
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];
|
return $this->fields[$name];
|
||||||
}
|
}
|
||||||
@ -145,7 +146,7 @@ class ObjectType extends Type implements OutputType, CompositeType
|
|||||||
foreach ($interfaces as $iface) {
|
foreach ($interfaces as $iface) {
|
||||||
$iface = Type::resolve($iface);
|
$iface = Type::resolve($iface);
|
||||||
if (!$iface instanceof InterfaceType) {
|
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;
|
$this->interfaces[] = $iface;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace GraphQL\Tests\Type;
|
namespace GraphQL\Tests\Type;
|
||||||
|
|
||||||
use GraphQL\Error\InvariantViolation;
|
use GraphQL\Error\UserError;
|
||||||
use GraphQL\Type\Definition\Type;
|
use GraphQL\Type\Definition\Type;
|
||||||
|
|
||||||
class ScalarSerializationTest extends \PHPUnit_Framework_TestCase
|
class ScalarSerializationTest extends \PHPUnit_Framework_TestCase
|
||||||
@ -28,28 +28,28 @@ class ScalarSerializationTest extends \PHPUnit_Framework_TestCase
|
|||||||
try {
|
try {
|
||||||
$intType->serialize(9876504321);
|
$intType->serialize(9876504321);
|
||||||
$this->fail('Expected exception was not thrown');
|
$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());
|
$this->assertEquals('Int cannot represent non 32-bit signed integer value: 9876504321', $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$intType->serialize(-9876504321);
|
$intType->serialize(-9876504321);
|
||||||
$this->fail('Expected exception was not thrown');
|
$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());
|
$this->assertEquals('Int cannot represent non 32-bit signed integer value: -9876504321', $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$intType->serialize(1e100);
|
$intType->serialize(1e100);
|
||||||
$this->fail('Expected exception was not thrown');
|
$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());
|
$this->assertEquals('Int cannot represent non 32-bit signed integer value: 1.0E+100', $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$intType->serialize(-1e100);
|
$intType->serialize(-1e100);
|
||||||
$this->fail('Expected exception was not thrown');
|
$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());
|
$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 {
|
try {
|
||||||
$intType->serialize('one');
|
$intType->serialize('one');
|
||||||
$this->fail('Expected exception was not thrown');
|
$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());
|
$this->assertEquals('Int cannot represent non 32-bit signed integer value: one', $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$intType->serialize('');
|
$intType->serialize('');
|
||||||
$this->fail('Expected exception was not thrown');
|
$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());
|
$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 {
|
try {
|
||||||
$floatType->serialize('one');
|
$floatType->serialize('one');
|
||||||
$this->fail('Expected exception was not thrown');
|
$this->fail('Expected exception was not thrown');
|
||||||
} catch (InvariantViolation $e) {
|
} catch (UserError $e) {
|
||||||
$this->assertEquals('Float cannot represent non numeric value: one', $e->getMessage());
|
$this->assertEquals('Float cannot represent non numeric value: one', $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$floatType->serialize('');
|
$floatType->serialize('');
|
||||||
$this->fail('Expected exception was not thrown');
|
$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());
|
$this->assertEquals('Float cannot represent non numeric value: (empty string)', $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user