|
10 | 10 | use Exception;
|
11 | 11 | use GraphQL\Error\FormattedError;
|
12 | 12 | use GraphQL\Error\SyntaxError;
|
| 13 | +use GraphQL\Language\Source; |
13 | 14 | use Magento\Framework\App\Area;
|
14 | 15 | use Magento\Framework\App\AreaList;
|
15 | 16 | use Magento\Framework\App\FrontControllerInterface;
|
@@ -239,25 +240,21 @@ public function dispatch(RequestInterface $request): ResponseInterface
|
239 | 240 | /**
|
240 | 241 | * Handle GraphQL Exceptions
|
241 | 242 | *
|
242 |
| - * @param Exception $error |
| 243 | + * @param Exception $e |
243 | 244 | * @return array
|
244 | 245 | * @throws Throwable
|
245 | 246 | */
|
246 |
| - private function handleGraphQlException(Exception $error): array |
| 247 | + private function handleGraphQlException(Exception $e): array |
247 | 248 | {
|
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]; |
261 | 258 | }
|
262 | 259 |
|
263 | 260 | /**
|
@@ -286,23 +283,30 @@ private function getHttpResponseCode(array $result): int
|
286 | 283 | *
|
287 | 284 | * @param RequestInterface $request
|
288 | 285 | * @return array
|
289 |
| - * @throws GraphQlInputException |
| 286 | + * @throws SyntaxError |
290 | 287 | */
|
291 | 288 | private function getDataFromRequest(RequestInterface $request): array
|
292 | 289 | {
|
293 | 290 | $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 { |
300 | 303 | $data['variables'] = !empty($data['variables']) && is_string($data['variables'])
|
301 | 304 | ? $this->jsonSerializer->unserialize($data['variables'])
|
302 | 305 | : null;
|
| 306 | + } catch (\InvalidArgumentException $e) { |
| 307 | + $source = new Source($data['variables']); |
| 308 | + throw new SyntaxError($source, 0, $e->getMessage()); |
303 | 309 | }
|
304 |
| - } catch (\InvalidArgumentException $e) { |
305 |
| - throw new GraphQlInputException(__('Unable to parse the request.'), $e); |
306 | 310 | }
|
307 | 311 |
|
308 | 312 | return $data;
|
|
0 commit comments