Skip to content

Commit

Permalink
Clean up error messages (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Oct 20, 2022
1 parent 72ce768 commit 0e71c35
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/DateScalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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}");
}

Expand Down
7 changes: 3 additions & 4 deletions src/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/NullScalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}.";
}
}
5 changes: 2 additions & 3 deletions src/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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}.";
}
}
2 changes: 0 additions & 2 deletions src/StringScalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
5 changes: 1 addition & 4 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion tests/MixedScalarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ protected function executeQueryWithJsonVariable(string $jsonLiteral): ExecutionR
"var": $jsonLiteral
}
JSON
,
,
true
);

Expand Down
4 changes: 1 addition & 3 deletions tests/NullScalarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0e71c35

Please sign in to comment.