File tree Expand file tree Collapse file tree 1 file changed +12
-3
lines changed
Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change 66
77use PhpList \RestBundle \Entity \RequestInterface ;
88use Symfony \Component \HttpFoundation \Request ;
9+ use Symfony \Component \HttpKernel \Exception \UnprocessableEntityHttpException ;
910use Symfony \Component \Serializer \SerializerInterface ;
1011use Symfony \Component \Validator \Validator \ValidatorInterface ;
1112use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
13+ use Throwable ;
1214
1315class 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 ;
You can’t perform that action at this time.
0 commit comments