From a18b0e194ba8520d1898ebb0827d0bee5a188290 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Thu, 17 Oct 2019 10:26:19 -0500 Subject: [PATCH 1/2] magento graphql-ce#970 Cannot return several errors for one GraphQL request --- .../Exception/GraphQlInputException.php | 31 +++++++++++++++++-- .../Framework/GraphQl/Query/ErrorHandler.php | 25 +++++++++------ 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/lib/internal/Magento/Framework/GraphQl/Exception/GraphQlInputException.php b/lib/internal/Magento/Framework/GraphQl/Exception/GraphQlInputException.php index 429b7c04b747..11aad3d6f7f9 100644 --- a/lib/internal/Magento/Framework/GraphQl/Exception/GraphQlInputException.php +++ b/lib/internal/Magento/Framework/GraphQl/Exception/GraphQlInputException.php @@ -7,13 +7,15 @@ namespace Magento\Framework\GraphQl\Exception; -use Magento\Framework\Exception\InputException; +use Magento\Framework\Exception\AggregateExceptionInterface; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Phrase; +use GraphQL\Error\ClientAware; /** * Exception for GraphQL to be thrown when user supplies invalid input */ -class GraphQlInputException extends InputException implements \GraphQL\Error\ClientAware +class GraphQlInputException extends LocalizedException implements AggregateExceptionInterface, ClientAware { const EXCEPTION_CATEGORY = 'graphql-input'; @@ -22,6 +24,13 @@ class GraphQlInputException extends InputException implements \GraphQL\Error\Cli */ private $isSafe; + /** + * The array of errors that have been added via the addError() method + * + * @var \Magento\Framework\Exception\LocalizedException[] + */ + private $errors = []; + /** * Initialize object * @@ -51,4 +60,22 @@ public function getCategory() : string { return self::EXCEPTION_CATEGORY; } + + /** + * @param LocalizedException $exception + * @return $this + */ + public function addError(LocalizedException $exception): self + { + $this->errors[] = $exception; + return $this; + } + + /** + * @return LocalizedException[] + */ + public function getErrors(): array + { + return $this->errors; + } } diff --git a/lib/internal/Magento/Framework/GraphQl/Query/ErrorHandler.php b/lib/internal/Magento/Framework/GraphQl/Query/ErrorHandler.php index 2661034116f9..b3d78790892f 100644 --- a/lib/internal/Magento/Framework/GraphQl/Query/ErrorHandler.php +++ b/lib/internal/Magento/Framework/GraphQl/Query/ErrorHandler.php @@ -7,7 +7,7 @@ namespace Magento\Framework\GraphQl\Query; -use GraphQL\Error\ClientAware; +use Magento\Framework\Exception\AggregateExceptionInterface; use Psr\Log\LoggerInterface; /** @@ -36,13 +36,20 @@ public function __construct( */ public function handle(array $errors, callable $formatter): array { - return array_map( - function (ClientAware $error) use ($formatter) { - $this->logger->error($error); - - return $formatter($error); - }, - $errors - ); + $formattedErrors = []; + foreach ($errors as $error) { + $this->logger->error($error); + $previousError = $error->getPrevious(); + if ($previousError instanceof AggregateExceptionInterface && !empty($previousError->getErrors())) { + $aggregatedErrors = $previousError->getErrors(); + foreach ($aggregatedErrors as $aggregatedError) { + $this->logger->error($aggregatedError); + $formattedErrors[] = $formatter($aggregatedError); + } + } else { + $formattedErrors[] = $formatter($error); + } + } + return $formattedErrors; } } From 39042ac2e2afa306730134d35bde9a4c33e12340 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Thu, 17 Oct 2019 11:00:51 -0500 Subject: [PATCH 2/2] magento/graphql-ce#970 Cannot return several errors for one GraphQL request --- .../Framework/GraphQl/Exception/GraphQlInputException.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/internal/Magento/Framework/GraphQl/Exception/GraphQlInputException.php b/lib/internal/Magento/Framework/GraphQl/Exception/GraphQlInputException.php index 11aad3d6f7f9..28b91c753c7e 100644 --- a/lib/internal/Magento/Framework/GraphQl/Exception/GraphQlInputException.php +++ b/lib/internal/Magento/Framework/GraphQl/Exception/GraphQlInputException.php @@ -62,6 +62,8 @@ public function getCategory() : string } /** + * Add child error if used as aggregate exception + * * @param LocalizedException $exception * @return $this */ @@ -72,6 +74,8 @@ public function addError(LocalizedException $exception): self } /** + * Get child errors if used as aggregate exception + * * @return LocalizedException[] */ public function getErrors(): array