Skip to content

Commit 0e71c35

Browse files
authored
Clean up error messages (#21)
1 parent 72ce768 commit 0e71c35

File tree

10 files changed

+21
-25
lines changed

10 files changed

+21
-25
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
## v5.4.1
11+
12+
### Changed
13+
14+
- Clean up error messages
15+
1016
## v5.4.0
1117

1218
### Added

src/DateScalar.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,12 @@ protected function tryParsingDate($value, string $exceptionClass): DateTimeInter
5555
if (is_string($value)) {
5656
if (1 !== preg_match(static::regex(), $value, $matches)) {
5757
$regex = static::regex();
58-
throw new $exceptionClass("Value \"${value}\" does not match \"{$regex}\". Make sure it's ISO 8601 compliant ");
58+
throw new $exceptionClass("Value \"{$value}\" does not match \"{$regex}\". Make sure it's ISO 8601 compliant ");
5959
}
6060

6161
if (! $this->validateDate($matches['date'])) {
6262
$safeValue = Utils::printSafeJson($value);
63-
64-
throw new $exceptionClass("Expected input value to be ISO 8601 compliant. Given invalid value \"{$safeValue}\"");
63+
throw new $exceptionClass("Given input value is not ISO 8601 compliant: {$safeValue}.");
6564
}
6665

6766
try {
@@ -72,7 +71,6 @@ protected function tryParsingDate($value, string $exceptionClass): DateTimeInter
7271
}
7372

7473
$safeValue = Utils::printSafeJson($value);
75-
7674
throw new $exceptionClass("Cannot parse non-string into date: {$safeValue}");
7775
}
7876

src/JSON.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace MLL\GraphQLScalars;
44

55
use GraphQL\Error\Error;
6+
use GraphQL\Language\Printer;
67
use GraphQL\Type\Definition\ScalarType;
7-
use GraphQL\Utils\Utils as GraphQLUtils;
88
use Safe\Exceptions\JsonException;
99

1010
class JSON extends ScalarType
@@ -25,9 +25,8 @@ public function parseValue($value)
2525
public function parseLiteral($valueNode, ?array $variables = null)
2626
{
2727
if (! property_exists($valueNode, 'value')) {
28-
throw new Error(
29-
'Can only parse literals that contain a value, got ' . GraphQLUtils::printSafeJson($valueNode)
30-
);
28+
$withoutValue = Printer::doPrint($valueNode);
29+
throw new Error("Can not parse literals without a value: {$withoutValue}.");
3130
}
3231

3332
return $this->decodeJSON($valueNode->value);

src/NullScalar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public function parseLiteral($valueNode, ?array $variables = null)
4848
*/
4949
public static function notNullMessage($value): string
5050
{
51-
$notNull = Utils::printSafe($value);
51+
$notNull = Utils::printSafeJson($value);
5252

53-
return "Expected null, got: {$notNull}";
53+
return "Expected null, got: {$notNull}.";
5454
}
5555
}

src/Regex.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ abstract class Regex extends ScalarType
1616
* @param string $name the name that the scalar type will have in the schema
1717
* @param string|null $description a description for the type
1818
* @param string $regex the regular expression that is validated against
19-
*
20-
* @return Regex
2119
*/
2220
public static function make(string $name, ?string $description, string $regex): self
2321
{
@@ -102,7 +100,8 @@ public function parseLiteral($valueNode, ?array $variables = null): string
102100
public static function unmatchedRegexMessage(string $value): string
103101
{
104102
$safeValue = GraphQLUtils::printSafeJson($value);
103+
$regex = static::regex();
105104

106-
return "The given value {$safeValue} did not match the regex " . static::regex();
105+
return "The given value {$safeValue} did not match the regex {$regex}.";
107106
}
108107
}

src/StringScalar.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ abstract class StringScalar extends ScalarType
1515
* @param string $name the name that the scalar type will have in the schema
1616
* @param string|null $description a description for the type
1717
* @param callable(string): bool $isValid a function that returns a boolean whether a given string is valid
18-
*
19-
* @return StringScalar
2018
*/
2119
public static function make(string $name, ?string $description, callable $isValid): self
2220
{

src/Utils.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ public static function coerceToString($value, string $exceptionClass): string
5555
{
5656
if (! self::canBeString($value)) {
5757
$safeValue = GraphQLUtils::printSafeJson($value);
58-
59-
throw new $exceptionClass(
60-
"The given value {$safeValue} can not be coerced to a string."
61-
);
58+
throw new $exceptionClass("The given value can not be coerced to a string: {$safeValue}.");
6259
}
6360

6461
// @phpstan-ignore-next-line we have proven the value can be safely cast

tests/EmailTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ final class EmailTest extends TestCase
1212
{
1313
public function testSerializeThrowsIfUnserializableValueIsGiven(): void
1414
{
15-
$this->expectException(InvariantViolation::class);
16-
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/^The given value .* can not be coerced to a string\./');
15+
$this->expectExceptionObject(new InvariantViolation(
16+
'The given value can not be coerced to a string: object.'
17+
));
1718

1819
(new Email())->serialize(
1920
new class() {

tests/MixedScalarTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ protected function executeQueryWithJsonVariable(string $jsonLiteral): ExecutionR
186186
"var": $jsonLiteral
187187
}
188188
JSON
189-
,
189+
,
190190
true
191191
);
192192

tests/NullScalarTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,12 @@ public function testAllowsNullArguments(): void
6464
public function testForbidsNonNullArguments(): void
6565
{
6666
$graphqlResult = $this->executeQueryWithLiteral('1');
67-
// @phpstan-ignore-next-line graphql-php is wrong
6867
self::assertNull($graphqlResult->data);
6968
self::assertSame('Field "foo" argument "bar" requires type Null, found 1.', $graphqlResult->errors[0]->getMessage());
7069

7170
$jsonResult = $this->executeQueryWithJsonVariable('1');
72-
// @phpstan-ignore-next-line graphql-php is wrong
7371
self::assertNull($jsonResult->data);
74-
self::assertSame('Variable "$var" got invalid value 1; Expected type Null; Expected null, got: 1', $jsonResult->errors[0]->getMessage());
72+
self::assertSame('Variable "$var" got invalid value 1; Expected type Null; Expected null, got: 1.', $jsonResult->errors[0]->getMessage());
7573
}
7674

7775
public function testForbidsNonNullReturn(): void

0 commit comments

Comments
 (0)