Skip to content

Commit 36e5479

Browse files
authored
Merge pull request #50 from 21TORR/log-json
2 parents e1979a2 + 7d0ebe8 commit 36e5479

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
3.3.1 (unreleased)
2+
=====
3+
4+
* (improvement) Log raw JSON when fetching the JSON content from a request fails.
5+
6+
17
3.3.0
28
=====
39

src/Controller/BaseController.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Psr\Log\LoggerInterface;
66
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
77
use Symfony\Component\Form\FormInterface;
8+
use Symfony\Component\HttpFoundation\Exception\JsonException;
89
use Symfony\Component\HttpFoundation\Request;
910
use Torr\Rad\Exception\Request\InvalidJsonRequestException;
1011
use Torr\Rad\Form\FormErrorNormalizer;
@@ -72,29 +73,18 @@ public function fetchJsonRequestBody (Request $request, bool $allowInvalid = fal
7273
throw new InvalidJsonRequestException("Expected JSON request content type.", 415);
7374
}
7475

75-
$raw = trim((string) $request->getContent());
76-
77-
if ("" === $raw)
78-
{
79-
return [];
80-
}
81-
8276
try
8377
{
84-
$data = json_decode($raw, true, 512, \JSON_THROW_ON_ERROR);
85-
86-
if (!\is_array($data))
87-
{
88-
throw new InvalidJsonRequestException(
89-
\sprintf("Invalid top level type in JSON payload. Must be array, is %s", \gettype($data)),
90-
400,
91-
);
92-
}
93-
94-
return $data;
78+
return $request->getPayload()->all();
9579
}
96-
catch (\JsonException $exception)
80+
catch (JsonException $exception)
9781
{
82+
$this->getLogger()->error("Parsing JSON payload failed: {message}", [
83+
"message" => $exception->getMessage(),
84+
"exception" => $exception,
85+
"json" => (string) $request->getContent(),
86+
]);
87+
9888
throw new InvalidJsonRequestException(
9989
\sprintf("Parsing JSON payload failed: %s", $exception->getMessage()),
10090
400,

0 commit comments

Comments
 (0)