From 57abcaa4df77590a6bf23b45bea94498e2ad6885 Mon Sep 17 00:00:00 2001 From: Andrii Date: Wed, 1 Jan 2025 21:07:40 +0100 Subject: [PATCH] :package: Refactor to avoid using additional kernel exception listener --- EventListener/ExceptionListener.php | 41 ----------- src/Controller/GraphQLiteController.php | 73 ++++++++++++++++--- src/Resources/config/container/graphqlite.xml | 4 - 3 files changed, 61 insertions(+), 57 deletions(-) delete mode 100644 EventListener/ExceptionListener.php diff --git a/EventListener/ExceptionListener.php b/EventListener/ExceptionListener.php deleted file mode 100644 index 8041621..0000000 --- a/EventListener/ExceptionListener.php +++ /dev/null @@ -1,41 +0,0 @@ -httpCodeDecider = $httpCodeDecider ?? new HttpCodeDecider(); - } - - public function onKernelException(ExceptionEvent $event): void - { - $exception = $event->getThrowable(); - - if ($exception instanceof GraphQLExceptionInterface) { - $result = new ExecutionResult( - errors: [new Error( - message: $exception->getMessage(), - previous: $exception, - extensions: $exception->getExtensions() - )] - ); - - $response = new JsonResponse($result->toArray(), $this->httpCodeDecider->decideHttpStatusCode($result)); - - $event->setResponse($response); - } - } -} diff --git a/src/Controller/GraphQLiteController.php b/src/Controller/GraphQLiteController.php index 28bb261..8cc2cdc 100644 --- a/src/Controller/GraphQLiteController.php +++ b/src/Controller/GraphQLiteController.php @@ -1,9 +1,15 @@ getMessage(), - code: Response::HTTP_UNSUPPORTED_MEDIA_TYPE, - previous:$e - ); + return $this->invalidJsonBodyResponse($e); } if (!is_array($parsedBody)) { - throw JsonException::create( - reason: 'Expecting associative array from request, got ' . gettype($parsedBody), - code: Response::HTTP_UNPROCESSABLE_ENTITY - ); + return $this->invalidRequestBodyExpectedAssociativeResponse($parsedBody); } + $psr7Request = $psr7Request->withParsedBody($parsedBody); } @@ -139,4 +141,51 @@ private function handlePsr7Request(ServerRequestInterface $request, Request $sym throw new RuntimeException('Only SyncPromiseAdapter is supported'); } + + private function invalidJsonBodyResponse(\JsonException $e): JsonResponse + { + $jsonException = JsonException::create( + reason: $e->getMessage(), + code: Response::HTTP_UNSUPPORTED_MEDIA_TYPE, + previous: $e, + ); + $result = new ExecutionResult( + null, + [ + new Error( + 'Invalid JSON.', + previous: $jsonException, + extensions: $jsonException->getExtensions(), + ), + ] + ); + + return new JsonResponse( + $result->toArray($this->debug), + $this->httpCodeDecider->decideHttpStatusCode($result) + ); + } + + private function invalidRequestBodyExpectedAssociativeResponse(mixed $parsedBody): JsonResponse + { + $jsonException = JsonException::create( + reason: 'Expecting associative array from request, got ' . gettype($parsedBody), + code: Response::HTTP_UNPROCESSABLE_ENTITY, + ); + $result = new ExecutionResult( + null, + [ + new Error( + 'Invalid JSON.', + previous: $jsonException, + extensions: $jsonException->getExtensions(), + ), + ] + ); + + return new JsonResponse( + $result->toArray($this->debug), + $this->httpCodeDecider->decideHttpStatusCode($result) + ); + } } diff --git a/src/Resources/config/container/graphqlite.xml b/src/Resources/config/container/graphqlite.xml index caa09d8..545ec0e 100644 --- a/src/Resources/config/container/graphqlite.xml +++ b/src/Resources/config/container/graphqlite.xml @@ -87,10 +87,6 @@ - - - -