diff --git a/CHANGELOG.md b/CHANGELOG.md index a3d9b69..2113dd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## v5.4.1 + +### Changed + +- Clean up error messages + ## v5.4.0 ### Added diff --git a/src/DateScalar.php b/src/DateScalar.php index 2d2ec3b..a691655 100644 --- a/src/DateScalar.php +++ b/src/DateScalar.php @@ -55,13 +55,12 @@ protected function tryParsingDate($value, string $exceptionClass): DateTimeInter if (is_string($value)) { if (1 !== preg_match(static::regex(), $value, $matches)) { $regex = static::regex(); - throw new $exceptionClass("Value \"${value}\" does not match \"{$regex}\". Make sure it's ISO 8601 compliant "); + throw new $exceptionClass("Value \"{$value}\" does not match \"{$regex}\". Make sure it's ISO 8601 compliant "); } if (! $this->validateDate($matches['date'])) { $safeValue = Utils::printSafeJson($value); - - throw new $exceptionClass("Expected input value to be ISO 8601 compliant. Given invalid value \"{$safeValue}\""); + throw new $exceptionClass("Given input value is not ISO 8601 compliant: {$safeValue}."); } try { @@ -72,7 +71,6 @@ protected function tryParsingDate($value, string $exceptionClass): DateTimeInter } $safeValue = Utils::printSafeJson($value); - throw new $exceptionClass("Cannot parse non-string into date: {$safeValue}"); } diff --git a/src/JSON.php b/src/JSON.php index 34a6903..fb8ca15 100644 --- a/src/JSON.php +++ b/src/JSON.php @@ -3,8 +3,8 @@ namespace MLL\GraphQLScalars; use GraphQL\Error\Error; +use GraphQL\Language\Printer; use GraphQL\Type\Definition\ScalarType; -use GraphQL\Utils\Utils as GraphQLUtils; use Safe\Exceptions\JsonException; class JSON extends ScalarType @@ -25,9 +25,8 @@ public function parseValue($value) public function parseLiteral($valueNode, ?array $variables = null) { if (! property_exists($valueNode, 'value')) { - throw new Error( - 'Can only parse literals that contain a value, got ' . GraphQLUtils::printSafeJson($valueNode) - ); + $withoutValue = Printer::doPrint($valueNode); + throw new Error("Can not parse literals without a value: {$withoutValue}."); } return $this->decodeJSON($valueNode->value); diff --git a/src/NullScalar.php b/src/NullScalar.php index a3896db..be575ab 100644 --- a/src/NullScalar.php +++ b/src/NullScalar.php @@ -48,8 +48,8 @@ public function parseLiteral($valueNode, ?array $variables = null) */ public static function notNullMessage($value): string { - $notNull = Utils::printSafe($value); + $notNull = Utils::printSafeJson($value); - return "Expected null, got: {$notNull}"; + return "Expected null, got: {$notNull}."; } } diff --git a/src/Regex.php b/src/Regex.php index 9678dd0..51f9346 100644 --- a/src/Regex.php +++ b/src/Regex.php @@ -16,8 +16,6 @@ abstract class Regex extends ScalarType * @param string $name the name that the scalar type will have in the schema * @param string|null $description a description for the type * @param string $regex the regular expression that is validated against - * - * @return Regex */ public static function make(string $name, ?string $description, string $regex): self { @@ -102,7 +100,8 @@ public function parseLiteral($valueNode, ?array $variables = null): string public static function unmatchedRegexMessage(string $value): string { $safeValue = GraphQLUtils::printSafeJson($value); + $regex = static::regex(); - return "The given value {$safeValue} did not match the regex " . static::regex(); + return "The given value {$safeValue} did not match the regex {$regex}."; } } diff --git a/src/StringScalar.php b/src/StringScalar.php index a69b75c..f3cfee7 100644 --- a/src/StringScalar.php +++ b/src/StringScalar.php @@ -15,8 +15,6 @@ abstract class StringScalar extends ScalarType * @param string $name the name that the scalar type will have in the schema * @param string|null $description a description for the type * @param callable(string): bool $isValid a function that returns a boolean whether a given string is valid - * - * @return StringScalar */ public static function make(string $name, ?string $description, callable $isValid): self { diff --git a/src/Utils.php b/src/Utils.php index cb96392..5dc83b2 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -55,10 +55,7 @@ public static function coerceToString($value, string $exceptionClass): string { if (! self::canBeString($value)) { $safeValue = GraphQLUtils::printSafeJson($value); - - throw new $exceptionClass( - "The given value {$safeValue} can not be coerced to a string." - ); + throw new $exceptionClass("The given value can not be coerced to a string: {$safeValue}."); } // @phpstan-ignore-next-line we have proven the value can be safely cast diff --git a/tests/EmailTest.php b/tests/EmailTest.php index a162066..4908dd1 100644 --- a/tests/EmailTest.php +++ b/tests/EmailTest.php @@ -12,8 +12,9 @@ final class EmailTest extends TestCase { public function testSerializeThrowsIfUnserializableValueIsGiven(): void { - $this->expectException(InvariantViolation::class); - $this->expectExceptionMessageMatches(/** @lang RegExp */ '/^The given value .* can not be coerced to a string\./'); + $this->expectExceptionObject(new InvariantViolation( + 'The given value can not be coerced to a string: object.' + )); (new Email())->serialize( new class() { diff --git a/tests/MixedScalarTest.php b/tests/MixedScalarTest.php index 0cfdf51..c9f04cf 100644 --- a/tests/MixedScalarTest.php +++ b/tests/MixedScalarTest.php @@ -186,7 +186,7 @@ protected function executeQueryWithJsonVariable(string $jsonLiteral): ExecutionR "var": $jsonLiteral } JSON -, + , true ); diff --git a/tests/NullScalarTest.php b/tests/NullScalarTest.php index e251186..d953bf8 100644 --- a/tests/NullScalarTest.php +++ b/tests/NullScalarTest.php @@ -64,14 +64,12 @@ public function testAllowsNullArguments(): void public function testForbidsNonNullArguments(): void { $graphqlResult = $this->executeQueryWithLiteral('1'); - // @phpstan-ignore-next-line graphql-php is wrong self::assertNull($graphqlResult->data); self::assertSame('Field "foo" argument "bar" requires type Null, found 1.', $graphqlResult->errors[0]->getMessage()); $jsonResult = $this->executeQueryWithJsonVariable('1'); - // @phpstan-ignore-next-line graphql-php is wrong self::assertNull($jsonResult->data); - self::assertSame('Variable "$var" got invalid value 1; Expected type Null; Expected null, got: 1', $jsonResult->errors[0]->getMessage()); + self::assertSame('Variable "$var" got invalid value 1; Expected type Null; Expected null, got: 1.', $jsonResult->errors[0]->getMessage()); } public function testForbidsNonNullReturn(): void