Skip to content

Commit f835179

Browse files
committed
ACP2E-4194: GraphQL Exception in SWAT
1 parent 835e075 commit f835179

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

app/code/Magento/GraphQl/Controller/GraphQl.php

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Exception;
1111
use GraphQL\Error\FormattedError;
1212
use GraphQL\Error\SyntaxError;
13+
use GraphQL\Language\Source;
1314
use Magento\Framework\App\Area;
1415
use Magento\Framework\App\AreaList;
1516
use Magento\Framework\App\FrontControllerInterface;
@@ -239,25 +240,21 @@ public function dispatch(RequestInterface $request): ResponseInterface
239240
/**
240241
* Handle GraphQL Exceptions
241242
*
242-
* @param Exception $error
243+
* @param Exception $e
243244
* @return array
244245
* @throws Throwable
245246
*/
246-
private function handleGraphQlException(Exception $error): array
247+
private function handleGraphQlException(Exception $e): array
247248
{
248-
if ($error instanceof SyntaxError || $error instanceof GraphQlInputException) {
249-
return [['errors' => [FormattedError::createFromException($error)]], 400];
250-
}
251-
if ($error instanceof GraphQlAuthenticationException) {
252-
return [['errors' => [$this->graphQlError->create($error)]], 401];
253-
}
254-
if ($error instanceof GraphQlAuthorizationException) {
255-
return [['errors' => [$this->graphQlError->create($error)]], 403];
256-
}
257-
return [
258-
['errors' => [$this->graphQlError->create($error)]],
259-
ExceptionFormatter::HTTP_GRAPH_QL_SCHEMA_ERROR_STATUS
260-
];
249+
[$error, $statusCode] = match (true) {
250+
$e instanceof GraphQlInputException => [FormattedError::createFromException($e), 200],
251+
$e instanceof SyntaxError => [FormattedError::createFromException($e), 400],
252+
$e instanceof GraphQlAuthenticationException => [$this->graphQlError->create($e), 401],
253+
$e instanceof GraphQlAuthorizationException => [$this->graphQlError->create($e), 403],
254+
default => [$this->graphQlError->create($e), ExceptionFormatter::HTTP_GRAPH_QL_SCHEMA_ERROR_STATUS],
255+
};
256+
257+
return [['errors' => [$error]], $statusCode];
261258
}
262259

263260
/**
@@ -286,23 +283,30 @@ private function getHttpResponseCode(array $result): int
286283
*
287284
* @param RequestInterface $request
288285
* @return array
289-
* @throws GraphQlInputException
286+
* @throws SyntaxError
290287
*/
291288
private function getDataFromRequest(RequestInterface $request): array
292289
{
293290
$data = [];
294-
try {
295-
/** @var Http $request */
296-
if ($request->isPost()) {
297-
$data = $request->getContent() ? $this->jsonSerializer->unserialize($request->getContent()) : [];
298-
} elseif ($request->isGet()) {
299-
$data = $request->getParams();
291+
/** @var Http $request */
292+
if ($request->isPost() && $request->getContent()) {
293+
$content = $request->getContent();
294+
try {
295+
$data = $this->jsonSerializer->unserialize($content);
296+
} catch (\InvalidArgumentException $e) {
297+
$source = new Source($content);
298+
throw new SyntaxError($source, 0, $e->getMessage());
299+
}
300+
} elseif ($request->isGet()) {
301+
$data = $request->getParams();
302+
try {
300303
$data['variables'] = !empty($data['variables']) && is_string($data['variables'])
301304
? $this->jsonSerializer->unserialize($data['variables'])
302305
: null;
306+
} catch (\InvalidArgumentException $e) {
307+
$source = new Source($data['variables']);
308+
throw new SyntaxError($source, 0, $e->getMessage());
303309
}
304-
} catch (\InvalidArgumentException $e) {
305-
throw new GraphQlInputException(__('Unable to parse the request.'), $e);
306310
}
307311

308312
return $data;

app/design/adminhtml/Magento/backend/i18n/en_US.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,4 +550,4 @@ Dashboard,Dashboard
550550
"Email to a Friend","Email to a Friend"
551551
"Invalid data type","Invalid data type"
552552
"Invalid value provided for attribute %1","Invalid value provided for attribute %1"
553-
553+
"Unknown type ""%1"".","Unknown type ""%1""."

lib/internal/Magento/Framework/GraphQl/Schema/SchemaGenerator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\Framework\GraphQl\Schema;
99

1010
use Magento\Framework\GraphQl\ConfigInterface;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1112
use Magento\Framework\GraphQl\Schema;
1213
use Magento\Framework\GraphQl\Schema\Type\TypeRegistry;
1314
use Magento\Framework\GraphQl\SchemaFactory;
@@ -57,7 +58,11 @@ public function generate() : Schema
5758
'query' => $this->typeRegistry->get('Query'),
5859
'mutation' => $this->typeRegistry->get('Mutation'),
5960
'typeLoader' => function ($name) {
60-
return $this->typeRegistry->get($name);
61+
try {
62+
return $this->typeRegistry->get($name);
63+
} catch (GraphQlInputException) {
64+
return null;
65+
}
6166
},
6267
'types' => function () {
6368
$typesImplementors = [];

lib/internal/Magento/Framework/GraphQl/Schema/Type/TypeRegistry.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Framework\GraphQl\Schema\Type;
99

10+
use LogicException;
1011
use Magento\Framework\GraphQl\ConfigInterface;
1112
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1213
use Magento\Framework\GraphQl\Schema\TypeInterface;
@@ -66,7 +67,13 @@ public function __construct(
6667
public function get(string $typeName): TypeInterface
6768
{
6869
if (!isset($this->types[$typeName])) {
69-
$configElement = $this->config->getConfigElement($typeName);
70+
try {
71+
$configElement = $this->config->getConfigElement($typeName);
72+
} catch (LogicException) {
73+
throw new GraphQlInputException(
74+
new Phrase('Unknown type "%1".', [$typeName])
75+
);
76+
}
7077

7178
$configElementClass = get_class($configElement);
7279
if (!isset($this->configToTypeMap[$configElementClass])) {

0 commit comments

Comments
 (0)