['onKernelException', 256], ]; } public function onKernelException(ExceptionEvent $event): void { $exception = $this->findUniqueConstraintViolation($event->getThrowable()); if (!$exception) { return; } $constraint = $this->detectConstraintName($exception); $error = match ($constraint) { 'unique_category_name' => 'Un élément avec ce nom existe déjà dans cette catégorie.', default => 'Un élément avec cette valeur existe déjà.', }; $event->setResponse(new JsonResponse( [ 'success' => false, 'error' => $error, 'constraint' => $constraint, ], JsonResponse::HTTP_CONFLICT )); } private function findUniqueConstraintViolation(Throwable $throwable): ?UniqueConstraintViolationException { for ($current = $throwable; null !== $current; $current = $current->getPrevious()) { if ($current instanceof UniqueConstraintViolationException) { return $current; } } return null; } private function detectConstraintName(UniqueConstraintViolationException $exception): ?string { $message = $exception->getMessage(); if (preg_match('/constraint\s+"([^"]+)"/', $message, $matches)) { return $matches[1]; } return null; } }