|
1 | 1 | <?php |
2 | 2 |
|
3 | | - |
4 | 3 | namespace TheCodingMachine\GraphQLite\Bundle\Controller; |
5 | 4 |
|
6 | | - |
7 | 5 | use GraphQL\Executor\ExecutionResult; |
8 | 6 | use GraphQL\Server\ServerConfig; |
9 | 7 | use GraphQL\Server\StandardServer; |
|
22 | 20 | use Symfony\Component\Routing\Route; |
23 | 21 | use Symfony\Component\Routing\RouteCollection; |
24 | 22 | use TheCodingMachine\GraphQLite\Bundle\Context\SymfonyGraphQLContext; |
| 23 | +use TheCodingMachine\GraphQLite\Bundle\Exceptions\JsonException; |
25 | 24 | use TheCodingMachine\GraphQLite\Http\HttpCodeDecider; |
26 | 25 | use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface; |
| 26 | + |
27 | 27 | use function array_map; |
28 | 28 | use function class_exists; |
29 | 29 | use function json_decode; |
30 | | -use TheCodingMachine\GraphQLite\Bundle\Exceptions\JsonException; |
31 | 30 |
|
32 | 31 | /** |
33 | 32 | * Listens to every single request and forward Graphql requests to Graphql Webonix standardServer. |
@@ -88,19 +87,13 @@ public function handleRequest(Request $request): Response |
88 | 87 | flags: \JSON_THROW_ON_ERROR |
89 | 88 | ); |
90 | 89 | } 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); |
96 | 91 | } |
97 | 92 |
|
98 | 93 | 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); |
103 | 95 | } |
| 96 | + |
104 | 97 | $psr7Request = $psr7Request->withParsedBody($parsedBody); |
105 | 98 | } |
106 | 99 |
|
@@ -139,4 +132,51 @@ private function handlePsr7Request(ServerRequestInterface $request, Request $sym |
139 | 132 |
|
140 | 133 | throw new RuntimeException('Only SyncPromiseAdapter is supported'); |
141 | 134 | } |
| 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 | + } |
142 | 182 | } |
0 commit comments