From 03a0aba1e97a3fe1083aa7c17226385b2e90342c Mon Sep 17 00:00:00 2001 From: Bastian Waidelich Date: Tue, 12 Mar 2024 18:46:05 +0100 Subject: [PATCH] FEATURE: Improved error handling Adds support for "parseable coerce exceptions" introduced with https://github.com/bwaidelich/types/releases/tag/1.2.0 --- Classes/GraphQLMiddleware.php | 4 ++++ Classes/Resolver.php | 3 +++ 2 files changed, 7 insertions(+) diff --git a/Classes/GraphQLMiddleware.php b/Classes/GraphQLMiddleware.php index eff7a00..84b76a6 100644 --- a/Classes/GraphQLMiddleware.php +++ b/Classes/GraphQLMiddleware.php @@ -27,6 +27,7 @@ use Psr\Http\Server\RequestHandlerInterface; use ReflectionClass; use Throwable; +use Wwwision\Types\Exception\CoerceException; use Wwwision\TypesGraphQL\GraphQLGenerator; use Wwwision\TypesGraphQL\Types\CustomResolvers; use function array_map; @@ -141,6 +142,9 @@ private function handleGraphQLError(Throwable $error, callable $formatter): arra $formattedError['extensions']['statusCode'] = $originalException->getStatusCode(); $formattedError['extensions']['referenceCode'] = $originalException->getReferenceCode(); } + if ($originalException?->getPrevious() instanceof CoerceException) { + $formattedError['extensions']['issues'] = $originalException->getPrevious()->issues; + } return $formattedError; } diff --git a/Classes/Resolver.php b/Classes/Resolver.php index 19b19d1..d1202a4 100644 --- a/Classes/Resolver.php +++ b/Classes/Resolver.php @@ -16,6 +16,7 @@ use GraphQL\Type\Definition\ResolveInfo; use InvalidArgumentException; use UnitEnum; +use Wwwision\Types\Exception\CoerceException; use Wwwision\Types\Parser; use Wwwision\Types\Schema\EnumCaseSchema; use Wwwision\Types\Schema\EnumSchema; @@ -118,6 +119,8 @@ private function convertArgument(string|bool|int|UnitEnum|array|null $argument, if ($className !== null) { try { return instantiate($className, $argument); + } catch (CoerceException $e) { + throw new RequestError($e->getMessage(), 1688654808, $e); } catch (InvalidArgumentException $e) { throw new RequestError(sprintf('Validation error for %s: %s', $argumentType, $e->getMessage()), 1688654808, $e); }