From b284b6d22a2556c33c0076e6d8ac700736a84593 Mon Sep 17 00:00:00 2001 From: Amoifr Date: Mon, 24 Aug 2026 11:21:03 +0200 Subject: [PATCH] feat(symfony): map UniqueConstraintViolationException to 422 by default --- src/Symfony/Bundle/DependencyInjection/Configuration.php | 2 ++ tests/Symfony/Bundle/DependencyInjection/ConfigurationTest.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/Symfony/Bundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/DependencyInjection/Configuration.php index 012f0f1aa2..fb070df050 100644 --- a/src/Symfony/Bundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/DependencyInjection/Configuration.php @@ -23,6 +23,7 @@ use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use Doctrine\ORM\OptimisticLockException; use GraphQL\GraphQL; use Symfony\Bundle\FrameworkBundle\Controller\ControllerHelper; @@ -592,6 +593,7 @@ private function addExceptionToStatusSection(ArrayNodeDefinition $rootNode): voi SerializerExceptionInterface::class => Response::HTTP_BAD_REQUEST, InvalidArgumentException::class => Response::HTTP_BAD_REQUEST, OptimisticLockException::class => Response::HTTP_CONFLICT, + UniqueConstraintViolationException::class => Response::HTTP_UNPROCESSABLE_ENTITY, ]) ->info('The list of exceptions mapped to their HTTP status code.') ->normalizeKeys(false) diff --git a/tests/Symfony/Bundle/DependencyInjection/ConfigurationTest.php b/tests/Symfony/Bundle/DependencyInjection/ConfigurationTest.php index fced5378b7..b1d0a51c44 100644 --- a/tests/Symfony/Bundle/DependencyInjection/ConfigurationTest.php +++ b/tests/Symfony/Bundle/DependencyInjection/ConfigurationTest.php @@ -15,6 +15,7 @@ use ApiPlatform\Metadata\Exception\InvalidArgumentException; use ApiPlatform\Symfony\Bundle\DependencyInjection\Configuration; +use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use Doctrine\ORM\OptimisticLockException; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -101,6 +102,7 @@ private function runDefaultConfigTests(array $doctrineIntegrationsToLoad = ['orm ExceptionInterface::class => Response::HTTP_BAD_REQUEST, InvalidArgumentException::class => Response::HTTP_BAD_REQUEST, OptimisticLockException::class => Response::HTTP_CONFLICT, + UniqueConstraintViolationException::class => Response::HTTP_UNPROCESSABLE_ENTITY, ], 'path_segment_name_generator' => 'api_platform.metadata.path_segment_name_generator.underscore', 'inflector' => 'api_platform.metadata.inflector',