Skip to content

Commit 154fdfe

Browse files
authored
Merge pull request #124 from mcg-web/use-dedicated-exception-for-type-parsing-error
Throw UserError vs InvariantViolationError for errors caused by client
2 parents 53edfa0 + 6d5b4e5 commit 154fdfe

File tree

5 files changed

+33
-18
lines changed

5 files changed

+33
-18
lines changed

src/Error/UserError.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
namespace GraphQL\Error;
3+
4+
/**
5+
* Class UserError
6+
*
7+
* Note:
8+
* Error that can be display safely to client...
9+
*
10+
* @package GraphQL\Error
11+
*/
12+
class UserError extends InvariantViolation
13+
{
14+
}

src/Type/Definition/FloatType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace GraphQL\Type\Definition;
33

4-
use GraphQL\Error\InvariantViolation;
4+
use GraphQL\Error\UserError;
55
use GraphQL\Language\AST\FloatValueNode;
66
use GraphQL\Language\AST\IntValueNode;
77
use GraphQL\Utils;
@@ -50,14 +50,14 @@ public function parseValue($value)
5050
private function coerceFloat($value)
5151
{
5252
if ($value === '') {
53-
throw new InvariantViolation(
53+
throw new UserError(
5454
'Float cannot represent non numeric value: (empty string)'
5555
);
5656
}
5757
if (is_numeric($value) || $value === true || $value === false) {
5858
return (float)$value;
5959
}
60-
throw new InvariantViolation('Float cannot represent non numeric value: ' . Utils::printSafe($value));
60+
throw new UserError(sprintf('Float cannot represent non numeric value: %s', Utils::printSafe($value)));
6161
}
6262

6363
/**

src/Type/Definition/IntType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace GraphQL\Type\Definition;
33

4-
use GraphQL\Error\InvariantViolation;
4+
use GraphQL\Error\UserError;
55
use GraphQL\Language\AST\IntValueNode;
66
use GraphQL\Language\AST\ValueNode;
77
use GraphQL\Utils;
@@ -57,7 +57,7 @@ public function parseValue($value)
5757
private function coerceInt($value)
5858
{
5959
if ($value === '') {
60-
throw new InvariantViolation(
60+
throw new UserError(
6161
'Int cannot represent non 32-bit signed integer value: (empty string)'
6262
);
6363
}
@@ -67,8 +67,8 @@ private function coerceInt($value)
6767
if (is_numeric($value) && $value <= self::MAX_INT && $value >= self::MIN_INT) {
6868
return (int) $value;
6969
}
70-
throw new InvariantViolation(
71-
'Int cannot represent non 32-bit signed integer value: ' . Utils::printSafe($value)
70+
throw new UserError(
71+
sprintf('Int cannot represent non 32-bit signed integer value: %s', Utils::printSafe($value))
7272
);
7373
}
7474

src/Type/Definition/ObjectType.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace GraphQL\Type\Definition;
33
use GraphQL\Error\InvariantViolation;
4+
use GraphQL\Error\UserError;
45
use GraphQL\Utils;
56

67

@@ -127,7 +128,7 @@ public function getField($name)
127128
$this->getFields();
128129
}
129130
if (!isset($this->fields[$name])) {
130-
throw new InvariantViolation(sprintf("Field '%s' is not defined for type '%s'", $name, $this->name));
131+
throw new UserError(sprintf("Field '%s' is not defined for type '%s'", $name, $this->name));
131132
}
132133
return $this->fields[$name];
133134
}
@@ -145,7 +146,7 @@ public function getInterfaces()
145146
foreach ($interfaces as $iface) {
146147
$iface = Type::resolve($iface);
147148
if (!$iface instanceof InterfaceType) {
148-
throw new InvariantViolation("Expecting interface type, got " . Utils::printSafe($iface));
149+
throw new InvariantViolation(sprintf('Expecting interface type, got %s', Utils::printSafe($iface)));
149150
}
150151
$this->interfaces[] = $iface;
151152
}

tests/Type/ScalarSerializationTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace GraphQL\Tests\Type;
33

4-
use GraphQL\Error\InvariantViolation;
4+
use GraphQL\Error\UserError;
55
use GraphQL\Type\Definition\Type;
66

77
class ScalarSerializationTest extends \PHPUnit_Framework_TestCase
@@ -28,28 +28,28 @@ public function testSerializesOutputInt()
2828
try {
2929
$intType->serialize(9876504321);
3030
$this->fail('Expected exception was not thrown');
31-
} catch (InvariantViolation $e) {
31+
} catch (UserError $e) {
3232
$this->assertEquals('Int cannot represent non 32-bit signed integer value: 9876504321', $e->getMessage());
3333
}
3434

3535
try {
3636
$intType->serialize(-9876504321);
3737
$this->fail('Expected exception was not thrown');
38-
} catch (InvariantViolation $e) {
38+
} catch (UserError $e) {
3939
$this->assertEquals('Int cannot represent non 32-bit signed integer value: -9876504321', $e->getMessage());
4040
}
4141

4242
try {
4343
$intType->serialize(1e100);
4444
$this->fail('Expected exception was not thrown');
45-
} catch (InvariantViolation $e) {
45+
} catch (UserError $e) {
4646
$this->assertEquals('Int cannot represent non 32-bit signed integer value: 1.0E+100', $e->getMessage());
4747
}
4848

4949
try {
5050
$intType->serialize(-1e100);
5151
$this->fail('Expected exception was not thrown');
52-
} catch (InvariantViolation $e) {
52+
} catch (UserError $e) {
5353
$this->assertEquals('Int cannot represent non 32-bit signed integer value: -1.0E+100', $e->getMessage());
5454
}
5555

@@ -58,14 +58,14 @@ public function testSerializesOutputInt()
5858
try {
5959
$intType->serialize('one');
6060
$this->fail('Expected exception was not thrown');
61-
} catch (InvariantViolation $e) {
61+
} catch (UserError $e) {
6262
$this->assertEquals('Int cannot represent non 32-bit signed integer value: one', $e->getMessage());
6363
}
6464

6565
try {
6666
$intType->serialize('');
6767
$this->fail('Expected exception was not thrown');
68-
} catch (InvariantViolation $e) {
68+
} catch (UserError $e) {
6969
$this->assertEquals('Int cannot represent non 32-bit signed integer value: (empty string)', $e->getMessage());
7070
}
7171

@@ -92,14 +92,14 @@ public function testSerializesOutputFloat()
9292
try {
9393
$floatType->serialize('one');
9494
$this->fail('Expected exception was not thrown');
95-
} catch (InvariantViolation $e) {
95+
} catch (UserError $e) {
9696
$this->assertEquals('Float cannot represent non numeric value: one', $e->getMessage());
9797
}
9898

9999
try {
100100
$floatType->serialize('');
101101
$this->fail('Expected exception was not thrown');
102-
} catch (InvariantViolation $e) {
102+
} catch (UserError $e) {
103103
$this->assertEquals('Float cannot represent non numeric value: (empty string)', $e->getMessage());
104104
}
105105

0 commit comments

Comments
 (0)