@@ -62,6 +62,9 @@ also validate the resulting object with Symfony Validation if you set validation
62
62
- A ` BadRequestHttpException ` will be thrown when the request or rather the resulting object is invalid according to the
63
63
Symfony Validation, the request body can't be deserialized, it contains invalid JSON or the hierarchy levels of the
64
64
request body of exceed 512.
65
+ - If you are using the [ ConstraintViolationErrorHandler] ( src/ErrorHandler/ConstraintViolationErrorHandler.php ) error handler, a
66
+ [ ConstraintViolationException] ( src/Exception/ConstraintViolationException.php ) will be thrown if the validation of your object
67
+ fails. You can also implement your own handler by implementing the [ ErrorHandlerInterface] ( src/ErrorHandler/ErrorHandlerInterface.php ) .
65
68
- Currently, only JSON is supported as payload format and the payload is only taken from the requests body.
66
69
67
70
### How to use?
@@ -84,20 +87,14 @@ needed by the serializer.
84
87
85
88
final class UpdateFooDto
86
89
{
87
- /**
88
- * @Assert\N otNull(message="Id should not be null.")
89
- * @Assert\P ositive(message="Id should be a positive integer.")
90
- */
90
+ #[Assert\N otNull]
91
+ #[Assert\P ositive]
91
92
private int $id;
92
93
93
- /**
94
- * @Assert\N otBlank(message="Client version should not be be blank.")
95
- */
94
+ #[Assert\N otBlank]
96
95
private string $clientVersion;
97
96
98
- /**
99
- * @Assert\N otNull(message="Browser info should not be null.")
100
- */
97
+ #[Assert\N otNull]
101
98
private array $browserInfo;
102
99
103
100
public function getClientVersion(): string
@@ -187,12 +184,64 @@ final class FooController extends AbstractController
187
184
}
188
185
` ` `
189
186
190
- # ### Error handler
187
+ # ### Error handling
188
+
189
+ The extension provides a default error handler (`http-kernel-extensions/src/ErrorHandler/ConstraintViolationErrorHandler.php`) which
190
+ handles common de-normalization errors that should be considered type errors. It will create a
191
+ ` Fusonic\H ttpKernelExtensions\E xception\C onstraintViolationException` [ConstraintViolationException](src/Exception/ConstraintViolationException.php)
192
+ which can be used with the provided `Fusonic\HttpKernelExtensions\Normalizer\ConstraintViolationExceptionNormalizer` [ConstraintViolationExceptionNormalizer](src/Normalizer/ConstraintViolationExceptionNormalizer.php).
193
+ This normalizer is uses on Symfony's built-in `Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer` and enhances it
194
+ with some extra information : an `errorCode` and `messageTemplate`. Both useful for parsing validation errors on the client side.
195
+ If that does not match your needs you can simply provide your own error handler by implementing
196
+ the `Fusonic\HttpKernelExtensions\ErrorHandler\ErrorHandlerInterface` and passing it to the `RequestDtoResolver`.
197
+
198
+ You have to register the normalizer as a service like this :
199
+
200
+ ` ` ` yaml
201
+ Fusonic\H ttpKernelExtensions\N ormalizer\C onstraintViolationExceptionNormalizer:
202
+ arguments:
203
+ - "@serializer.normalizer.constraint_violation_list"
204
+ tags:
205
+ - { name: serializer.normalizer }
206
+ ` ` `
207
+
208
+ # #### Using an exception listener/subscriber
209
+ In Symfony you can use an exception listener or subscriber to eventually convert the `ConstraintViolationException` into an actual response using
210
+ the `Fusonic\HttpKernelExtensions\Normalizer\ConstraintViolationExceptionNormalizer`. For example :
211
+
212
+ ` ` ` php
213
+
214
+ use Symfony\C omponent\S erializer\N ormalizer\N ormalizerInterface;
215
+ use Symfony\C omponent\E ventDispatcher\E ventSubscriberInterface;
216
+ use Fusonic\H ttpKernelExtensions\E xception\C onstraintViolationException;
217
+ use Symfony\C omponent\H ttpKernel\E vent\E xceptionEvent;
218
+
219
+ final class ExceptionSubscriber implements EventSubscriberInterface {
220
+
221
+ public function __construct(private NormalizerInterface $normalizer)
222
+ {
223
+ }
224
+
225
+ public static function getSubscribedEvents(): array
226
+ {
227
+ return [
228
+ KernelEvents::EXCEPTION => 'onKernelException',
229
+ ];
230
+ }
231
+
232
+ public function onKernelException(ExceptionEvent $event): void
233
+ {
234
+ $throwable = $event->getThrowable();
235
+
236
+ if ($throwable instanceof ConstraintViolationException) {
237
+ $data = $this->normalizer->normalize($throwable);
238
+ $event->setResponse(new JsonResponse($data, 422));
239
+ }
240
+ }
241
+ }
242
+ ` ` `
191
243
192
- The extension provides a default error handler in here `http-kernel-extensions/src/ErrorHandler/ErrorHandler.php` which
193
- throws `BadRequestHttpExceptions` in case the request can't be deserialized onto the given class or Symfony Validation
194
- deems it invalid. If that does not match your needs you can simply provide your own error handler by implementing
195
- the `ErrorHandlerInterface` and passing it to the `RequestDtoResolver`.
244
+ Check the [Events and Event Listeners](https://symfony.com/doc/current/event_dispatcher.html) for details.
196
245
197
246
# ### ContextAwareProvider
198
247
0 commit comments