Skip to content

Commit

Permalink
improved feedback on unique key violation
Browse files Browse the repository at this point in the history
  • Loading branch information
danigargar committed Feb 14, 2024
1 parent 793a918 commit 762c379
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down

0 comments on commit 762c379

Please sign in to comment.