Skip to content

Commit 57abcaa

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

File tree

3 files changed

+61
-57
lines changed

3 files changed

+61
-57
lines changed

EventListener/ExceptionListener.php

-41
This file was deleted.

src/Controller/GraphQLiteController.php

+61-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
<?php
22

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

6-
5+
use GraphQL\Error\Error;
6+
use Laminas\Diactoros\ResponseFactory;
7+
use Laminas\Diactoros\ServerRequestFactory;
8+
use Laminas\Diactoros\StreamFactory;
9+
use Laminas\Diactoros\UploadedFileFactory;
10+
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
11+
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
12+
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
713
use GraphQL\Executor\ExecutionResult;
814
use GraphQL\Server\ServerConfig;
915
use GraphQL\Server\StandardServer;
@@ -24,10 +30,12 @@
2430
use TheCodingMachine\GraphQLite\Bundle\Context\SymfonyGraphQLContext;
2531
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
2632
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
33+
use TheCodingMachine\GraphQLite\Bundle\Exceptions\JsonException;
34+
35+
use function array_map;
2736
use function array_map;
2837
use function class_exists;
2938
use function json_decode;
30-
use TheCodingMachine\GraphQLite\Bundle\Exceptions\JsonException;
3139

3240
/**
3341
* Listens to every single request and forward Graphql requests to Graphql Webonix standardServer.
@@ -88,19 +96,13 @@ public function handleRequest(Request $request): Response
8896
flags: \JSON_THROW_ON_ERROR
8997
);
9098
} catch (\JsonException $e) {
91-
throw JsonException::create(
92-
reason: $e->getMessage(),
93-
code: Response::HTTP_UNSUPPORTED_MEDIA_TYPE,
94-
previous:$e
95-
);
99+
return $this->invalidJsonBodyResponse($e);
96100
}
97101

98102
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-
);
103+
return $this->invalidRequestBodyExpectedAssociativeResponse($parsedBody);
103104
}
105+
104106
$psr7Request = $psr7Request->withParsedBody($parsedBody);
105107
}
106108

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

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

src/Resources/config/container/graphqlite.xml

-4
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)