diff --git a/CHANGELOG.md b/CHANGELOG.md index ce08c3772e..06cbf8ed36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ You can find and compare releases at the [GitHub release page](https://github.co ## Unreleased +### Fixed + +- Throw client-safe errors for malformed date literals instead of reporting them as server errors https://github.com/nuwave/lighthouse/pull/2788 + ## v6.70.0 ### Changed diff --git a/src/Schema/Types/Scalars/DateScalar.php b/src/Schema/Types/Scalars/DateScalar.php index 55fbf9e229..54eebe3b45 100644 --- a/src/Schema/Types/Scalars/DateScalar.php +++ b/src/Schema/Types/Scalars/DateScalar.php @@ -45,7 +45,7 @@ public function parseLiteral(Node $valueNode, ?array $variables = null): Illumin try { return $this->parse($value); } catch (\Exception $exception) { - throw Error::createLocatedError($exception, $valueNode); + throw new Error($exception->getMessage(), $valueNode); } } diff --git a/tests/Unit/Schema/Types/Scalars/DateScalarTestBase.php b/tests/Unit/Schema/Types/Scalars/DateScalarTestBase.php index 021f289d45..80fe4d5a9a 100644 --- a/tests/Unit/Schema/Types/Scalars/DateScalarTestBase.php +++ b/tests/Unit/Schema/Types/Scalars/DateScalarTestBase.php @@ -107,6 +107,25 @@ public function testThrowsIfParseLiteralNonString(): void ); } + public function testThrowsClientSafeErrorIfParseLiteralInvalidDate(): void + { + $error = null; + + try { + $this->scalarInstance()->parseLiteral( + new StringValueNode(['value' => 'rolf']), + ); + } catch (Error $caught) { + $error = $caught; + } + + $this->assertInstanceOf(Error::class, $error); + $this->assertTrue( + $error->isClientSafe(), + 'A malformed date literal is client misuse, so it must not be reported as a server error.', + ); + } + public function testSerializesCarbonInstance(): void { $now = IlluminateCarbon::now();