13
13
14
14
use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
15
15
use Symfony \Component \HttpFoundation \Request ;
16
- use Symfony \Component \HttpFoundation \Response ;
17
16
use Symfony \Component \HttpKernel \Attribute \MapQueryString ;
18
17
use Symfony \Component \HttpKernel \Attribute \MapRequestPayload ;
19
18
use Symfony \Component \HttpKernel \Controller \ValueResolverInterface ;
20
19
use Symfony \Component \HttpKernel \ControllerMetadata \ArgumentMetadata ;
21
20
use Symfony \Component \HttpKernel \Event \ControllerArgumentsEvent ;
21
+ use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
22
22
use Symfony \Component \HttpKernel \Exception \HttpException ;
23
+ use Symfony \Component \HttpKernel \Exception \UnsupportedMediaTypeHttpException ;
23
24
use Symfony \Component \HttpKernel \KernelEvents ;
24
25
use Symfony \Component \Serializer \Exception \NotEncodableValueException ;
25
26
use Symfony \Component \Serializer \Exception \PartialDenormalizationException ;
@@ -124,21 +125,21 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event): vo
124
125
}
125
126
126
127
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 ));
128
129
}
129
130
} else {
130
131
try {
131
132
$ payload = $ this ->$ payloadMapper ($ request , $ type , $ argument );
132
133
} 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 );
134
135
}
135
136
}
136
137
137
138
if (null === $ payload ) {
138
139
$ payload = match (true ) {
139
140
$ argument ->metadata ->hasDefaultValue () => $ argument ->metadata ->getDefaultValue (),
140
141
$ argument ->metadata ->isNullable () => null ,
141
- default => throw new HttpException ($ validationFailedCode )
142
+ default => throw HttpException:: fromStatusCode ($ validationFailedCode )
142
143
};
143
144
}
144
145
@@ -167,11 +168,11 @@ private function mapQueryString(Request $request, string $type, MapQueryString $
167
168
private function mapRequestPayload (Request $ request , string $ type , MapRequestPayload $ attribute ): ?object
168
169
{
169
170
if (null === $ format = $ request ->getContentTypeFormat ()) {
170
- throw new HttpException (Response:: HTTP_UNSUPPORTED_MEDIA_TYPE , 'Unsupported format. ' );
171
+ throw new UnsupportedMediaTypeHttpException ( 'Unsupported format. ' );
171
172
}
172
173
173
174
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 ));
175
176
}
176
177
177
178
if ($ data = $ request ->request ->all ()) {
@@ -183,15 +184,15 @@ private function mapRequestPayload(Request $request, string $type, MapRequestPay
183
184
}
184
185
185
186
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. ' );
187
188
}
188
189
189
190
try {
190
191
return $ this ->serializer ->deserialize ($ data , $ type , $ format , self ::CONTEXT_DESERIALIZE + $ attribute ->serializationContext );
191
192
} 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 );
193
194
} 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 );
195
196
}
196
197
}
197
198
}
0 commit comments