Skip to content

Commit 3365eaf

Browse files
simPodspawnia
authored andcommitted
feat: do not override ClientSafe exception with CoercionError
1 parent c1c9e5b commit 3365eaf

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/Utils/Value.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace GraphQL\Utils;
44

5+
use GraphQL\Error\ClientAware;
56
use GraphQL\Error\CoercionError;
67
use GraphQL\Error\Error;
78
use GraphQL\Error\InvariantViolation;
@@ -61,7 +62,10 @@ public static function coerceInputValue($value, InputType $type, ?array $path =
6162
try {
6263
return self::ofValue($type->parseValue($value));
6364
} catch (\Throwable $error) {
64-
if ($error instanceof Error) {
65+
if (
66+
$error instanceof Error
67+
|| ($error instanceof ClientAware && $error->isClientSafe())
68+
) {
6569
return self::ofErrors([
6670
CoercionError::make($error->getMessage(), $path, $value, $error),
6771
]);

tests/Utils/CoerceInputValueTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace GraphQL\Tests\Utils;
44

5+
use GraphQL\Error\ClientAware;
56
use GraphQL\Error\CoercionError;
67
use GraphQL\Error\InvariantViolation;
78
use GraphQL\Type\Definition\CustomScalarType;
@@ -177,6 +178,34 @@ public function testReturnsErrorForIncorrectValueType(): void
177178
)]);
178179
}
179180

181+
public function testPropagatesClientSafeError(): void
182+
{
183+
$message = 'message';
184+
$value = ['value' => 1];
185+
186+
$clientSafeException = new class($message) extends \Exception implements ClientAware {
187+
public function isClientSafe(): bool
188+
{
189+
return true;
190+
}
191+
};
192+
193+
$scalar = new CustomScalarType([
194+
'name' => 'TestScalar',
195+
'parseValue' => function () use ($clientSafeException) {
196+
throw $clientSafeException;
197+
},
198+
'parseLiteral' => fn () => null,
199+
]);
200+
201+
$result = Value::coerceInputValue($value, $scalar);
202+
$this->expectGraphQLError($result, [CoercionError::make(
203+
$message,
204+
null,
205+
$value,
206+
)]);
207+
}
208+
180209
/**
181210
* @see describe('for GraphQLInputObject', () => {
182211
* @see it('returns no error for a valid input')

0 commit comments

Comments
 (0)