|
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; |
30 | 31 |
|
31 | 32 | /**
|
32 | 33 | * Listens to every single request and forward Graphql requests to Graphql Webonix standardServer.
|
@@ -80,12 +81,25 @@ 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 | + throw JsonException::create( |
| 92 | + reason: $e->getMessage(), |
| 93 | + code: Response::HTTP_UNSUPPORTED_MEDIA_TYPE, |
| 94 | + previous:$e |
| 95 | + ); |
86 | 96 | }
|
87 |
| - if (!is_array($parsedBody)){ |
88 |
| - throw new \RuntimeException('Expecting associative array from request, got ' . gettype($parsedBody)); |
| 97 | + |
| 98 | + 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 | + ); |
89 | 103 | }
|
90 | 104 | $psr7Request = $psr7Request->withParsedBody($parsedBody);
|
91 | 105 | }
|
|
0 commit comments