Skip to content

Commit

Permalink
Merge pull request #29 from bwaidelich/feature/improve-error-handling
Browse files Browse the repository at this point in the history
FEATURE: Improved error handling
  • Loading branch information
bwaidelich authored Mar 12, 2024
2 parents a14ca87 + 03a0aba commit 563b345
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Classes/GraphQLMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions Classes/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 563b345

Please sign in to comment.