feat: add UniqueConstraintViolationException#9979
feat: add UniqueConstraintViolationException#9979michalsn wants to merge 8 commits intocodeigniter4:4.8from
UniqueConstraintViolationException#9979Conversation
UniqueConstraintViolationException and getLastException()UniqueConstraintViolationException
| ClassPropertyAssignToConstructorPromotionRector::class => [ | ||
| __DIR__ . '/system/Database/BaseResult.php', | ||
| __DIR__ . '/system/Database/RawSql.php', | ||
| __DIR__ . '/system/Database/Exceptions/DatabaseException.php', |
There was a problem hiding this comment.
Is this related to the $databaseCode property? If yes, why not proceed to promote it as it is new?
There was a problem hiding this comment.
Rector was proposing this:
public function __construct(string $message = '', protected int|string $databaseCode = 0, ?Throwable $previous = null)Although named parameters are not part of our BC promise, this class extends RuntimeException, and its constructor defines the second argument as $code. So changing this introduces an inconsistency with the parent signature without providing a meaningful benefit.
There was a problem hiding this comment.
I see. Why not just make the constructor parameter as $databaseCode already, so that it would not clash with $code which do not accept any native type. Like in https://3v4l.org/hS5D0#vnull
|
👋 Hi, @michalsn! |
Description
This PR adds:
UniqueConstraintViolationException(extendsDatabaseException), thrown on duplicate key violations across all database drivers$db->getLastException(): ?DatabaseException- whenDBDebugisfalse, exceptions are not thrown. This method provides a reliable way to inspect the type of failure, since raw error codes from$db->error()['code']differ across driversDatabaseException::getDatabaseCode(): int|string- exposes the native driver error code on the exception itself, asgetCode()is constrained tointby PHP Throwable interface and cannot carry string SQLSTATE codesgetLastException()is intentionally a transitional API. The long-term direction is to dropDBDebug = falseentirely (v5 ?), leaving exceptions as the only error signalling mechanism - consistent with how every other major PHP framework handles database errors.Checklist: