Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
3.3.1 (unreleased)
=====

* (improvement) Log raw JSON when fetching the JSON content from a request fails.


3.3.0
=====

Expand Down
28 changes: 9 additions & 19 deletions src/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Exception\JsonException;
use Symfony\Component\HttpFoundation\Request;
use Torr\Rad\Exception\Request\InvalidJsonRequestException;
use Torr\Rad\Form\FormErrorNormalizer;
Expand Down Expand Up @@ -72,29 +73,18 @@ public function fetchJsonRequestBody (Request $request, bool $allowInvalid = fal
throw new InvalidJsonRequestException("Expected JSON request content type.", 415);
}

$raw = trim((string) $request->getContent());

if ("" === $raw)
{
return [];
}

try
{
$data = json_decode($raw, true, 512, \JSON_THROW_ON_ERROR);

if (!\is_array($data))
{
throw new InvalidJsonRequestException(
\sprintf("Invalid top level type in JSON payload. Must be array, is %s", \gettype($data)),
400,
);
}

return $data;
return $request->getPayload()->all();
}
catch (\JsonException $exception)
catch (JsonException $exception)
{
$this->getLogger()->error("Parsing JSON payload failed: {message}", [
"message" => $exception->getMessage(),
"exception" => $exception,
"json" => (string) $request->getContent(),
]);

throw new InvalidJsonRequestException(
\sprintf("Parsing JSON payload failed: %s", $exception->getMessage()),
400,
Expand Down