Skip to content

Commit fba23aa

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

File tree

3 files changed

+52
-57
lines changed

3 files changed

+52
-57
lines changed

EventListener/ExceptionListener.php

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

src/Controller/GraphQLiteController.php

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

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

6-
75
use GraphQL\Executor\ExecutionResult;
86
use GraphQL\Server\ServerConfig;
97
use GraphQL\Server\StandardServer;
@@ -22,12 +20,13 @@
2220
use Symfony\Component\Routing\Route;
2321
use Symfony\Component\Routing\RouteCollection;
2422
use TheCodingMachine\GraphQLite\Bundle\Context\SymfonyGraphQLContext;
23+
use TheCodingMachine\GraphQLite\Bundle\Exceptions\JsonException;
2524
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
2625
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
26+
2727
use function array_map;
2828
use function class_exists;
2929
use function json_decode;
30-
use TheCodingMachine\GraphQLite\Bundle\Exceptions\JsonException;
3130

3231
/**
3332
* Listens to every single request and forward Graphql requests to Graphql Webonix standardServer.
@@ -88,19 +87,13 @@ public function handleRequest(Request $request): Response
8887
flags: \JSON_THROW_ON_ERROR
8988
);
9089
} catch (\JsonException $e) {
91-
throw JsonException::create(
92-
reason: $e->getMessage(),
93-
code: Response::HTTP_UNSUPPORTED_MEDIA_TYPE,
94-
previous:$e
95-
);
90+
return $this->invalidJsonBodyResponse($e);
9691
}
9792

9893
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-
);
94+
return $this->invalidRequestBodyExpectedAssociativeResponse($parsedBody);
10395
}
96+
10497
$psr7Request = $psr7Request->withParsedBody($parsedBody);
10598
}
10699

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

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

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)