Skip to content

Commit 253d39d

Browse files
committed
ISSUE-345: return 422
1 parent d716a9d commit 253d39d

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/Validator/RequestValidator.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
use PhpList\RestBundle\Entity\RequestInterface;
88
use Symfony\Component\HttpFoundation\Request;
9+
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
910
use Symfony\Component\Serializer\SerializerInterface;
1011
use Symfony\Component\Validator\Validator\ValidatorInterface;
1112
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
13+
use Throwable;
1214

1315
class RequestValidator
1416
{
@@ -19,12 +21,19 @@ public function __construct(
1921

2022
public function validate(Request $request, string $dtoClass): RequestInterface
2123
{
22-
$dto = $this->serializer->deserialize($request->getContent(), $dtoClass, 'json');
23-
24+
try {
25+
$dto = $this->serializer->deserialize(
26+
$request->getContent(),
27+
$dtoClass,
28+
'json'
29+
);
30+
} catch (Throwable $e) {
31+
throw new UnprocessableEntityHttpException('Invalid JSON: ' . $e->getMessage());
32+
}
2433
$errors = $this->validator->validate($dto);
2534

2635
if (count($errors) > 0) {
27-
throw new BadRequestHttpException((string) $errors);
36+
throw new UnprocessableEntityHttpException((string) $errors);
2837
}
2938

3039
return $dto;

0 commit comments

Comments
 (0)