Skip to content

Commit

Permalink
feat: do not override ClientSafe exception with CoercionError
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod authored and spawnia committed Oct 31, 2024
1 parent c1c9e5b commit 3365eaf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Utils/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace GraphQL\Utils;

use GraphQL\Error\ClientAware;
use GraphQL\Error\CoercionError;
use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
Expand Down Expand Up @@ -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),
]);
Expand Down
29 changes: 29 additions & 0 deletions tests/Utils/CoerceInputValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 3365eaf

Please sign in to comment.