|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| - |
4 | 3 | namespace TheCodingMachine\GraphQLite\Bundle\Controller;
|
5 | 4 |
|
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; |
11 | 6 | use Laminas\Diactoros\ResponseFactory;
|
12 | 7 | use Laminas\Diactoros\ServerRequestFactory;
|
13 | 8 | use Laminas\Diactoros\StreamFactory;
|
14 | 9 | 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; |
15 | 17 | use Psr\Http\Message\ServerRequestInterface;
|
16 | 18 | use RuntimeException;
|
17 |
| -use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; |
18 | 19 | use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
|
19 | 20 | use Symfony\Component\HttpFoundation\JsonResponse;
|
20 | 21 | use Symfony\Component\HttpFoundation\Request;
|
21 | 22 | use Symfony\Component\HttpFoundation\Response;
|
22 | 23 | use Symfony\Component\Routing\Route;
|
23 | 24 | use Symfony\Component\Routing\RouteCollection;
|
24 | 25 | 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 | + |
27 | 28 | use function array_map;
|
28 | 29 | use function class_exists;
|
29 | 30 | use function json_decode;
|
@@ -80,13 +81,20 @@ public function handleRequest(Request $request): Response
|
80 | 81 |
|
81 | 82 | if (strtoupper($request->getMethod()) === 'POST' && empty($psr7Request->getParsedBody())) {
|
82 | 83 | $content = $psr7Request->getBody()->getContents();
|
83 |
| - $parsedBody = json_decode($content, true); |
84 |
| - if (json_last_error() !== JSON_ERROR_NONE) { |
85 |
| - throw new \RuntimeException('Invalid JSON received in POST body: '.json_last_error_msg()); |
| 84 | + try { |
| 85 | + $parsedBody = json_decode( |
| 86 | + json: $content, |
| 87 | + associative: true, |
| 88 | + flags: \JSON_THROW_ON_ERROR |
| 89 | + ); |
| 90 | + } catch (\JsonException $e) { |
| 91 | + return $this->invalidJsonBodyResponse($e); |
86 | 92 | }
|
87 |
| - if (!is_array($parsedBody)){ |
88 |
| - throw new \RuntimeException('Expecting associative array from request, got ' . gettype($parsedBody)); |
| 93 | + |
| 94 | + if (!is_array($parsedBody)) { |
| 95 | + return $this->invalidRequestBodyExpectedAssociativeResponse($parsedBody); |
89 | 96 | }
|
| 97 | + |
90 | 98 | $psr7Request = $psr7Request->withParsedBody($parsedBody);
|
91 | 99 | }
|
92 | 100 |
|
@@ -125,4 +133,51 @@ private function handlePsr7Request(ServerRequestInterface $request, Request $sym
|
125 | 133 |
|
126 | 134 | throw new RuntimeException('Only SyncPromiseAdapter is supported');
|
127 | 135 | }
|
| 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 | + } |
128 | 183 | }
|
0 commit comments