Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
magento graphql-ce#970 Cannot return several errors for one GraphQL r…
Browse files Browse the repository at this point in the history
…equest
  • Loading branch information
lenaorobei committed Oct 17, 2019
1 parent d271281 commit a18b0e1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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
*
Expand Down Expand Up @@ -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;
}
}
25 changes: 16 additions & 9 deletions lib/internal/Magento/Framework/GraphQl/Query/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Magento\Framework\GraphQl\Query;

use GraphQL\Error\ClientAware;
use Magento\Framework\Exception\AggregateExceptionInterface;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -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;
}
}

0 comments on commit a18b0e1

Please sign in to comment.