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

magento graphql-ce#970 Cannot return several errors for one GraphQL request #1013

Merged
merged 2 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer an immutable type of objects. We can initialization to construct. But it's a recommendation but not a requirement.

{
$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;
}
}