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

Commit

Permalink
ENGCOM-6118: magento graphql-ce#970 Cannot return several errors for …
Browse files Browse the repository at this point in the history
…one GraphQL request #1013
  • Loading branch information
lenaorobei authored Oct 18, 2019
2 parents abbfa03 + 460e345 commit ab11289
Show file tree
Hide file tree
Showing 2 changed files with 49 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,26 @@ public function getCategory() : string
{
return self::EXCEPTION_CATEGORY;
}

/**
* Add child error if used as aggregate exception
*
* @param LocalizedException $exception
* @return $this
*/
public function addError(LocalizedException $exception): self
{
$this->errors[] = $exception;
return $this;
}

/**
* Get child errors if used as aggregate exception
*
* @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 ab11289

Please sign in to comment.