From 762c379b77e47a00adda0452c3a3d1931d9d4cb7 Mon Sep 17 00:00:00 2001 From: Dani Garcia Date: Wed, 14 Feb 2024 15:23:06 +0100 Subject: [PATCH] improved feedback on unique key violation --- .../Service/DuplicateEntryCommonErrorHandler.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Infrastructure/Persistence/Doctrine/Service/DuplicateEntryCommonErrorHandler.php b/Infrastructure/Persistence/Doctrine/Service/DuplicateEntryCommonErrorHandler.php index 6b566d3..261a7e5 100644 --- a/Infrastructure/Persistence/Doctrine/Service/DuplicateEntryCommonErrorHandler.php +++ b/Infrastructure/Persistence/Doctrine/Service/DuplicateEntryCommonErrorHandler.php @@ -37,8 +37,15 @@ public function handle(\Throwable $exception) $isDuplicatedError = $pdoException->getCode() === self::MYSQL_ERROR_DUPLICATE_ENTRY; if ($isDuplicatedError) { + preg_match( + '/Duplicate entry \'[_a-zA-Z0-9]+\' for key \'([_a-zA-Z0-9]+)\'/', + $exception->getMessage(), + $results + ); + $uniqueKey = $results[1] ?? 'unknown'; + throw new \DomainException( - 'Duplicated value found', + 'Duplicate value on key: ' . $uniqueKey, 0, $exception );