1313
1414use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
1515use Symfony \Component \HttpFoundation \Request ;
16- use Symfony \Component \HttpFoundation \Response ;
1716use Symfony \Component \HttpKernel \Attribute \MapQueryString ;
1817use Symfony \Component \HttpKernel \Attribute \MapRequestPayload ;
1918use Symfony \Component \HttpKernel \Controller \ValueResolverInterface ;
2019use Symfony \Component \HttpKernel \ControllerMetadata \ArgumentMetadata ;
2120use Symfony \Component \HttpKernel \Event \ControllerArgumentsEvent ;
21+ use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
2222use Symfony \Component \HttpKernel \Exception \HttpException ;
23+ use Symfony \Component \HttpKernel \Exception \UnsupportedMediaTypeHttpException ;
2324use Symfony \Component \HttpKernel \KernelEvents ;
2425use Symfony \Component \Serializer \Exception \NotEncodableValueException ;
2526use Symfony \Component \Serializer \Exception \PartialDenormalizationException ;
@@ -124,21 +125,21 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event): vo
124125 }
125126
126127 if (\count ($ violations )) {
127- throw new HttpException ($ validationFailedCode , implode ("\n" , array_map (static fn ($ e ) => $ e ->getMessage (), iterator_to_array ($ violations ))), new ValidationFailedException ($ payload , $ violations ));
128+ throw HttpException:: fromStatusCode ($ validationFailedCode , implode ("\n" , array_map (static fn ($ e ) => $ e ->getMessage (), iterator_to_array ($ violations ))), new ValidationFailedException ($ payload , $ violations ));
128129 }
129130 } else {
130131 try {
131132 $ payload = $ this ->$ payloadMapper ($ request , $ type , $ argument );
132133 } catch (PartialDenormalizationException $ e ) {
133- throw new HttpException ($ validationFailedCode , implode ("\n" , array_map (static fn ($ e ) => $ e ->getMessage (), $ e ->getErrors ())), $ e );
134+ throw HttpException:: fromStatusCode ($ validationFailedCode , implode ("\n" , array_map (static fn ($ e ) => $ e ->getMessage (), $ e ->getErrors ())), $ e );
134135 }
135136 }
136137
137138 if (null === $ payload ) {
138139 $ payload = match (true ) {
139140 $ argument ->metadata ->hasDefaultValue () => $ argument ->metadata ->getDefaultValue (),
140141 $ argument ->metadata ->isNullable () => null ,
141- default => throw new HttpException ($ validationFailedCode )
142+ default => throw HttpException:: fromStatusCode ($ validationFailedCode )
142143 };
143144 }
144145
@@ -167,11 +168,11 @@ private function mapQueryString(Request $request, string $type, MapQueryString $
167168 private function mapRequestPayload (Request $ request , string $ type , MapRequestPayload $ attribute ): ?object
168169 {
169170 if (null === $ format = $ request ->getContentTypeFormat ()) {
170- throw new HttpException (Response:: HTTP_UNSUPPORTED_MEDIA_TYPE , 'Unsupported format. ' );
171+ throw new UnsupportedMediaTypeHttpException ( 'Unsupported format. ' );
171172 }
172173
173174 if ($ attribute ->acceptFormat && !\in_array ($ format , (array ) $ attribute ->acceptFormat , true )) {
174- throw new HttpException (Response:: HTTP_UNSUPPORTED_MEDIA_TYPE , sprintf ('Unsupported format, expects "%s", but "%s" given. ' , implode ('", " ' , (array ) $ attribute ->acceptFormat ), $ format ));
175+ throw new UnsupportedMediaTypeHttpException ( sprintf ('Unsupported format, expects "%s", but "%s" given. ' , implode ('", " ' , (array ) $ attribute ->acceptFormat ), $ format ));
175176 }
176177
177178 if ($ data = $ request ->request ->all ()) {
@@ -183,15 +184,15 @@ private function mapRequestPayload(Request $request, string $type, MapRequestPay
183184 }
184185
185186 if ('form ' === $ format ) {
186- throw new HttpException (Response:: HTTP_BAD_REQUEST , 'Request payload contains invalid "form" data. ' );
187+ throw new BadRequestHttpException ( 'Request payload contains invalid "form" data. ' );
187188 }
188189
189190 try {
190191 return $ this ->serializer ->deserialize ($ data , $ type , $ format , self ::CONTEXT_DESERIALIZE + $ attribute ->serializationContext );
191192 } catch (UnsupportedFormatException $ e ) {
192- throw new HttpException (Response:: HTTP_UNSUPPORTED_MEDIA_TYPE , sprintf ('Unsupported format: "%s". ' , $ format ), $ e );
193+ throw new UnsupportedMediaTypeHttpException ( sprintf ('Unsupported format: "%s". ' , $ format ), $ e );
193194 } catch (NotEncodableValueException $ e ) {
194- throw new HttpException (Response:: HTTP_BAD_REQUEST , sprintf ('Request payload contains invalid "%s" data. ' , $ format ), $ e );
195+ throw new BadRequestHttpException ( sprintf ('Request payload contains invalid "%s" data. ' , $ format ), $ e );
195196 }
196197 }
197198}
0 commit comments