Skip to content

Commit ab11289

Browse files
authored
ENGCOM-6118: magento graphql-ce#970 Cannot return several errors for one GraphQL request #1013
2 parents abbfa03 + 460e345 commit ab11289

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

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

Lines changed: 33 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,26 @@ public function getCategory() : string
5160
{
5261
return self::EXCEPTION_CATEGORY;
5362
}
63+
64+
/**
65+
* Add child error if used as aggregate exception
66+
*
67+
* @param LocalizedException $exception
68+
* @return $this
69+
*/
70+
public function addError(LocalizedException $exception): self
71+
{
72+
$this->errors[] = $exception;
73+
return $this;
74+
}
75+
76+
/**
77+
* Get child errors if used as aggregate exception
78+
*
79+
* @return LocalizedException[]
80+
*/
81+
public function getErrors(): array
82+
{
83+
return $this->errors;
84+
}
5485
}

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)