Skip to content

Commit 32e189e

Browse files
committed
Throw client-safe errors for malformed date literals
DateScalar::parseLiteral passed the Carbon exception to Error::createLocatedError, which keeps it as the error's previous. Since InvalidFormatException is not ClientAware, that marked the error as non-client-safe: the message was masked as "Internal server error" and the exception was handed to the application's error reporter. The variable path already rethrows without a previous via tryParsingDate, so the same malformed value was a client error as a variable but a server fault as a literal. Bring literals in line.
1 parent 163bf6f commit 32e189e

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ You can find and compare releases at the [GitHub release page](https://github.co
99

1010
## Unreleased
1111

12+
### Fixed
13+
14+
- Throw client-safe errors for malformed date literals instead of reporting them as server errors https://github.com/nuwave/lighthouse/pull/2788
15+
1216
## v6.70.0
1317

1418
### Changed

src/Schema/Types/Scalars/DateScalar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function parseLiteral(Node $valueNode, ?array $variables = null): Illumin
4545
try {
4646
return $this->parse($value);
4747
} catch (\Exception $exception) {
48-
throw Error::createLocatedError($exception, $valueNode);
48+
throw new Error($exception->getMessage(), $valueNode);
4949
}
5050
}
5151

tests/Unit/Schema/Types/Scalars/DateScalarTestBase.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,25 @@ public function testThrowsIfParseLiteralNonString(): void
107107
);
108108
}
109109

110+
public function testThrowsClientSafeErrorIfParseLiteralInvalidDate(): void
111+
{
112+
$error = null;
113+
114+
try {
115+
$this->scalarInstance()->parseLiteral(
116+
new StringValueNode(['value' => 'rolf']),
117+
);
118+
} catch (Error $caught) {
119+
$error = $caught;
120+
}
121+
122+
$this->assertInstanceOf(Error::class, $error);
123+
$this->assertTrue(
124+
$error->isClientSafe(),
125+
'A malformed date literal is client misuse, so it must not be reported as a server error.',
126+
);
127+
}
128+
110129
public function testSerializesCarbonInstance(): void
111130
{
112131
$now = IlluminateCarbon::now();

0 commit comments

Comments
 (0)