Skip to content

Commit 80cdfbb

Browse files
andrew-dembcvergne
authored andcommitted
📦 Refactor to avoid using additional kernel exception listener
1 parent 03af0fe commit 80cdfbb

File tree

3 files changed

+60
-64
lines changed

3 files changed

+60
-64
lines changed

EventListener/ExceptionListener.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/Controller/GraphQLiteController.php

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
<?php
22

3-
43
namespace TheCodingMachine\GraphQLite\Bundle\Controller;
54

6-
7-
use GraphQL\Executor\ExecutionResult;
8-
use GraphQL\Server\ServerConfig;
9-
use GraphQL\Server\StandardServer;
10-
use GraphQL\Upload\UploadMiddleware;
5+
use GraphQL\Error\Error;
116
use Laminas\Diactoros\ResponseFactory;
127
use Laminas\Diactoros\ServerRequestFactory;
138
use Laminas\Diactoros\StreamFactory;
149
use Laminas\Diactoros\UploadedFileFactory;
10+
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
11+
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
12+
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
13+
use GraphQL\Executor\ExecutionResult;
14+
use GraphQL\Server\ServerConfig;
15+
use GraphQL\Server\StandardServer;
16+
use GraphQL\Upload\UploadMiddleware;
1517
use Psr\Http\Message\ServerRequestInterface;
1618
use RuntimeException;
17-
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
1819
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
1920
use Symfony\Component\HttpFoundation\JsonResponse;
2021
use Symfony\Component\HttpFoundation\Request;
2122
use Symfony\Component\HttpFoundation\Response;
2223
use Symfony\Component\Routing\Route;
2324
use Symfony\Component\Routing\RouteCollection;
2425
use TheCodingMachine\GraphQLite\Bundle\Context\SymfonyGraphQLContext;
25-
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
26-
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
26+
use TheCodingMachine\GraphQLite\Bundle\Exceptions\JsonException;
27+
2728
use function array_map;
2829
use function class_exists;
2930
use function json_decode;
30-
use TheCodingMachine\GraphQLite\Bundle\Exceptions\JsonException;
3131

3232
/**
3333
* Listens to every single request and forward Graphql requests to Graphql Webonix standardServer.
@@ -88,19 +88,13 @@ public function handleRequest(Request $request): Response
8888
flags: \JSON_THROW_ON_ERROR
8989
);
9090
} catch (\JsonException $e) {
91-
throw JsonException::create(
92-
reason: $e->getMessage(),
93-
code: Response::HTTP_UNSUPPORTED_MEDIA_TYPE,
94-
previous:$e
95-
);
91+
return $this->invalidJsonBodyResponse($e);
9692
}
9793

9894
if (!is_array($parsedBody)) {
99-
throw JsonException::create(
100-
reason: 'Expecting associative array from request, got ' . gettype($parsedBody),
101-
code: Response::HTTP_UNPROCESSABLE_ENTITY
102-
);
95+
return $this->invalidRequestBodyExpectedAssociativeResponse($parsedBody);
10396
}
97+
10498
$psr7Request = $psr7Request->withParsedBody($parsedBody);
10599
}
106100

@@ -139,4 +133,51 @@ private function handlePsr7Request(ServerRequestInterface $request, Request $sym
139133

140134
throw new RuntimeException('Only SyncPromiseAdapter is supported');
141135
}
136+
137+
private function invalidJsonBodyResponse(\JsonException $e): JsonResponse
138+
{
139+
$jsonException = JsonException::create(
140+
reason: $e->getMessage(),
141+
code: Response::HTTP_UNSUPPORTED_MEDIA_TYPE,
142+
previous: $e,
143+
);
144+
$result = new ExecutionResult(
145+
null,
146+
[
147+
new Error(
148+
'Invalid JSON.',
149+
previous: $jsonException,
150+
extensions: $jsonException->getExtensions(),
151+
),
152+
]
153+
);
154+
155+
return new JsonResponse(
156+
$result->toArray($this->debug),
157+
$this->httpCodeDecider->decideHttpStatusCode($result)
158+
);
159+
}
160+
161+
private function invalidRequestBodyExpectedAssociativeResponse(mixed $parsedBody): JsonResponse
162+
{
163+
$jsonException = JsonException::create(
164+
reason: 'Expecting associative array from request, got ' . gettype($parsedBody),
165+
code: Response::HTTP_UNPROCESSABLE_ENTITY,
166+
);
167+
$result = new ExecutionResult(
168+
null,
169+
[
170+
new Error(
171+
'Invalid JSON.',
172+
previous: $jsonException,
173+
extensions: $jsonException->getExtensions(),
174+
),
175+
]
176+
);
177+
178+
return new JsonResponse(
179+
$result->toArray($this->debug),
180+
$this->httpCodeDecider->decideHttpStatusCode($result)
181+
);
182+
}
142183
}

src/Resources/config/container/graphqlite.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@
8787
<tag name="routing.route_loader"/>
8888
</service>
8989

90-
<service id="TheCodingMachine\GraphQLite\Bundle\EventListener\ExceptionListener">
91-
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" />
92-
</service>
93-
9490
<service id="TheCodingMachine\GraphQLite\Bundle\Mappers\RequestParameterMiddleware">
9591
<tag name="graphql.parameter_middleware"/>
9692
</service>

0 commit comments

Comments
 (0)