1616use Symfony \Component \HttpKernel \Exception \AccessDeniedHttpException ;
1717use Symfony \Component \HttpKernel \Exception \HttpExceptionInterface ;
1818use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
19+ use Symfony \Component \Validator \Exception \ValidationFailedException ;
1920use Symfony \Component \Validator \Exception \ValidatorException ;
2021
2122class ExceptionListener
@@ -24,7 +25,6 @@ class ExceptionListener
2425 SubscriptionCreationException::class => null ,
2526 AttributeDefinitionCreationException::class => null ,
2627 AdminAttributeCreationException::class => null ,
27- ValidatorException::class => 400 ,
2828 AccessDeniedException::class => 403 ,
2929 AccessDeniedHttpException::class => 403 ,
3030 AttachmentFileNotFoundException::class => 404 ,
@@ -36,33 +36,82 @@ public function onKernelException(ExceptionEvent $event): void
3636 {
3737 $ exception = $ event ->getThrowable ();
3838
39+ if ($ exception instanceof ValidationFailedException || $ exception instanceof ValidatorException) {
40+ $ event ->setResponse (
41+ new JsonResponse ([
42+ 'message ' => 'Validation failed ' ,
43+ 'errors ' => $ this ->parseFlatValidationMessage ($ exception ->getMessage ()),
44+ ], 422 )
45+ );
46+
47+ return ;
48+ }
49+
3950 foreach (self ::EXCEPTION_STATUS_MAP as $ class => $ statusCode ) {
4051 if ($ exception instanceof $ class ) {
41- $ status = $ statusCode ?? $ exception ->getStatusCode ();
52+ $ status = $ statusCode ?? (
53+ method_exists ($ exception , 'getStatusCode ' )
54+ ? $ exception ->getStatusCode ()
55+ : 400
56+ );
57+
4258 $ event ->setResponse (
4359 new JsonResponse ([
44- 'message ' => $ exception ->getMessage ()
60+ 'message ' => $ exception ->getMessage (),
4561 ], $ status )
4662 );
63+
4764 return ;
4865 }
4966 }
5067
5168 if ($ exception instanceof HttpExceptionInterface) {
5269 $ event ->setResponse (
5370 new JsonResponse ([
54- 'message ' => $ exception ->getMessage ()
71+ 'message ' => $ exception ->getMessage (),
5572 ], $ exception ->getStatusCode ())
5673 );
74+
5775 return ;
5876 }
5977
6078 if ($ exception instanceof Exception) {
6179 $ event ->setResponse (
6280 new JsonResponse ([
63- 'message ' => $ exception ->getMessage ()
81+ 'message ' => $ exception ->getMessage (),
6482 ], 500 )
6583 );
6684 }
6785 }
86+
87+ /**
88+ * @return array<string, array<int, string>>
89+ */
90+ private function parseFlatValidationMessage (string $ message ): array
91+ {
92+ $ errors = [];
93+ $ lines = preg_split ('/\r\n|\r|\n/ ' , $ message ) ?: [];
94+
95+ foreach ($ lines as $ line ) {
96+ $ line = trim ($ line );
97+
98+ if ($ line === '' ) {
99+ continue ;
100+ }
101+
102+ $ parts = explode (': ' , $ line , 2 );
103+
104+ if (count ($ parts ) !== 2 ) {
105+ $ errors ['_global ' ][] = $ line ;
106+ continue ;
107+ }
108+
109+ $ field = trim ($ parts [0 ]);
110+ $ errorMessage = trim ($ parts [1 ]);
111+
112+ $ errors [$ field ][] = $ errorMessage ;
113+ }
114+
115+ return $ errors ;
116+ }
68117}
0 commit comments