Skip to content

Commit a18b0e1

Browse files
committed
magento graphql-ce#970 Cannot return several errors for one GraphQL request
1 parent d271281 commit a18b0e1

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

lib/internal/Magento/Framework/GraphQl/Exception/GraphQlInputException.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88
namespace Magento\Framework\GraphQl\Exception;
99

10-
use Magento\Framework\Exception\InputException;
10+
use Magento\Framework\Exception\AggregateExceptionInterface;
11+
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\Framework\Phrase;
13+
use GraphQL\Error\ClientAware;
1214

1315
/**
1416
* Exception for GraphQL to be thrown when user supplies invalid input
1517
*/
16-
class GraphQlInputException extends InputException implements \GraphQL\Error\ClientAware
18+
class GraphQlInputException extends LocalizedException implements AggregateExceptionInterface, ClientAware
1719
{
1820
const EXCEPTION_CATEGORY = 'graphql-input';
1921

@@ -22,6 +24,13 @@ class GraphQlInputException extends InputException implements \GraphQL\Error\Cli
2224
*/
2325
private $isSafe;
2426

27+
/**
28+
* The array of errors that have been added via the addError() method
29+
*
30+
* @var \Magento\Framework\Exception\LocalizedException[]
31+
*/
32+
private $errors = [];
33+
2534
/**
2635
* Initialize object
2736
*
@@ -51,4 +60,22 @@ public function getCategory() : string
5160
{
5261
return self::EXCEPTION_CATEGORY;
5362
}
63+
64+
/**
65+
* @param LocalizedException $exception
66+
* @return $this
67+
*/
68+
public function addError(LocalizedException $exception): self
69+
{
70+
$this->errors[] = $exception;
71+
return $this;
72+
}
73+
74+
/**
75+
* @return LocalizedException[]
76+
*/
77+
public function getErrors(): array
78+
{
79+
return $this->errors;
80+
}
5481
}

lib/internal/Magento/Framework/GraphQl/Query/ErrorHandler.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Magento\Framework\GraphQl\Query;
99

10-
use GraphQL\Error\ClientAware;
10+
use Magento\Framework\Exception\AggregateExceptionInterface;
1111
use Psr\Log\LoggerInterface;
1212

1313
/**
@@ -36,13 +36,20 @@ public function __construct(
3636
*/
3737
public function handle(array $errors, callable $formatter): array
3838
{
39-
return array_map(
40-
function (ClientAware $error) use ($formatter) {
41-
$this->logger->error($error);
42-
43-
return $formatter($error);
44-
},
45-
$errors
46-
);
39+
$formattedErrors = [];
40+
foreach ($errors as $error) {
41+
$this->logger->error($error);
42+
$previousError = $error->getPrevious();
43+
if ($previousError instanceof AggregateExceptionInterface && !empty($previousError->getErrors())) {
44+
$aggregatedErrors = $previousError->getErrors();
45+
foreach ($aggregatedErrors as $aggregatedError) {
46+
$this->logger->error($aggregatedError);
47+
$formattedErrors[] = $formatter($aggregatedError);
48+
}
49+
} else {
50+
$formattedErrors[] = $formatter($error);
51+
}
52+
}
53+
return $formattedErrors;
4754
}
4855
}

0 commit comments

Comments
 (0)