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 6
6
7
7
use PhpList \RestBundle \Entity \RequestInterface ;
8
8
use Symfony \Component \HttpFoundation \Request ;
9
+ use Symfony \Component \HttpKernel \Exception \UnprocessableEntityHttpException ;
9
10
use Symfony \Component \Serializer \SerializerInterface ;
10
11
use Symfony \Component \Validator \Validator \ValidatorInterface ;
11
12
use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
13
+ use Throwable ;
12
14
13
15
class RequestValidator
14
16
{
@@ -19,12 +21,19 @@ public function __construct(
19
21
20
22
public function validate (Request $ request , string $ dtoClass ): RequestInterface
21
23
{
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
+ }
24
33
$ errors = $ this ->validator ->validate ($ dto );
25
34
26
35
if (count ($ errors ) > 0 ) {
27
- throw new BadRequestHttpException ((string ) $ errors );
36
+ throw new UnprocessableEntityHttpException ((string ) $ errors );
28
37
}
29
38
30
39
return $ dto ;
You can’t perform that action at this time.
0 commit comments