From 3365eaf33c4472c23137328dce1316b79143ce85 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Wed, 30 Oct 2024 16:24:29 +0100 Subject: [PATCH] feat: do not override ClientSafe exception with CoercionError --- src/Utils/Value.php | 6 +++++- tests/Utils/CoerceInputValueTest.php | 29 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Utils/Value.php b/src/Utils/Value.php index ce6545a3f..6dd4713ad 100644 --- a/src/Utils/Value.php +++ b/src/Utils/Value.php @@ -2,6 +2,7 @@ namespace GraphQL\Utils; +use GraphQL\Error\ClientAware; use GraphQL\Error\CoercionError; use GraphQL\Error\Error; use GraphQL\Error\InvariantViolation; @@ -61,7 +62,10 @@ public static function coerceInputValue($value, InputType $type, ?array $path = try { return self::ofValue($type->parseValue($value)); } catch (\Throwable $error) { - if ($error instanceof Error) { + if ( + $error instanceof Error + || ($error instanceof ClientAware && $error->isClientSafe()) + ) { return self::ofErrors([ CoercionError::make($error->getMessage(), $path, $value, $error), ]); diff --git a/tests/Utils/CoerceInputValueTest.php b/tests/Utils/CoerceInputValueTest.php index 1ec4d3f25..082ce59ef 100644 --- a/tests/Utils/CoerceInputValueTest.php +++ b/tests/Utils/CoerceInputValueTest.php @@ -2,6 +2,7 @@ namespace GraphQL\Tests\Utils; +use GraphQL\Error\ClientAware; use GraphQL\Error\CoercionError; use GraphQL\Error\InvariantViolation; use GraphQL\Type\Definition\CustomScalarType; @@ -177,6 +178,34 @@ public function testReturnsErrorForIncorrectValueType(): void )]); } + public function testPropagatesClientSafeError(): void + { + $message = 'message'; + $value = ['value' => 1]; + + $clientSafeException = new class($message) extends \Exception implements ClientAware { + public function isClientSafe(): bool + { + return true; + } + }; + + $scalar = new CustomScalarType([ + 'name' => 'TestScalar', + 'parseValue' => function () use ($clientSafeException) { + throw $clientSafeException; + }, + 'parseLiteral' => fn () => null, + ]); + + $result = Value::coerceInputValue($value, $scalar); + $this->expectGraphQLError($result, [CoercionError::make( + $message, + null, + $value, + )]); + } + /** * @see describe('for GraphQLInputObject', () => { * @see it('returns no error for a valid input')