Skip to content

Core\Db\DbError::Kind

Says why the database refused a statement, in the same words on every backend.

Core\Db\DbError::Kind is declared in the class Core\Db\DbError. Its property kind has this type.

When a statement fails, the error your program catches carries one of these cases beside the server's own message. They are conditions rather than codes: a duplicate key is a duplicate key whether the server called it 23505, 1062 or SQLITE_CONSTRAINT_UNIQUE, so a program written against one database still reads correctly on the next. Some of the cases are worth acting on — a duplicate key is usually something to tell the person using your app about, and a deadlock is usually worth trying again. Other carries everything a driver's own table does not name.

Good to know: the server's raw SQLSTATE is still there beside the case, for the rare condition that matters to you and has no case of its own. Nothing is lost by the normalising; it only saves you from writing a table of vendor codes yourself.

Examples

What each refusal means

<?nvs
// The eleven reasons the database can reject a statement. Each reason is the
// same case on every database, so your program can check it without knowing
// which database it uses.
class Refusal
{
public static function meaning(Core\Db\DbError::Kind $kind): string
{
return match ($kind) {
Core\Db\DbError::Kind::UniqueViolation => 'UniqueViolation: a row with this key is already there',
Core\Db\DbError::Kind::ForeignKeyViolation => 'ForeignKeyViolation: the row it points at is missing, or something still points at this one',
Core\Db\DbError::Kind::NotNullViolation => 'NotNullViolation: a column that needs a value was left empty',
Core\Db\DbError::Kind::CheckViolation => 'CheckViolation: a rule written into the table refused the row',
Core\Db\DbError::Kind::Deadlock => 'Deadlock: two transactions each waited for the other, and this one was picked to lose',
Core\Db\DbError::Kind::SerializationFailure => 'SerializationFailure: another transaction changed the same rows first',
Core\Db\DbError::Kind::ConnectionLost => 'ConnectionLost: the connection is gone',
Core\Db\DbError::Kind::Timeout => 'Timeout: it ran too long and was cancelled',
Core\Db\DbError::Kind::Syntax => 'Syntax: the server will not run that statement at all',
Core\Db\DbError::Kind::Permission => 'Permission: this login may not do that',
Core\Db\DbError::Kind::Other => 'Other: anything the driver has no case of its own for',
};
}
}
array<Core\Db\DbError::Kind> $everything = [
Core\Db\DbError::Kind::UniqueViolation,
Core\Db\DbError::Kind::ForeignKeyViolation,
Core\Db\DbError::Kind::NotNullViolation,
Core\Db\DbError::Kind::CheckViolation,
Core\Db\DbError::Kind::Deadlock,
Core\Db\DbError::Kind::SerializationFailure,
Core\Db\DbError::Kind::ConnectionLost,
Core\Db\DbError::Kind::Timeout,
Core\Db\DbError::Kind::Syntax,
Core\Db\DbError::Kind::Permission,
Core\Db\DbError::Kind::Other,
];
foreach ($everything as Core\Db\DbError::Kind $kind) {
echo Refusal::meaning($kind), "\n";
}
Output
UniqueViolation: a row with this key is already there
ForeignKeyViolation: the row it points at is missing, or something still points at this one
NotNullViolation: a column that needs a value was left empty
CheckViolation: a rule written into the table refused the row
Deadlock: two transactions each waited for the other, and this one was picked to lose
SerializationFailure: another transaction changed the same rows first
ConnectionLost: the connection is gone
Timeout: it ran too long and was cancelled
Syntax: the server will not run that statement at all
Permission: this login may not do that
Other: anything the driver has no case of its own for

Telling the person what went wrong

<?nvs
// A sign-up form that failed. Some refusals are about what somebody typed and
// deserve a plain sentence on the screen. The rest are about your program or
// your server, and the person filling in the form can do nothing with them.
class SignUp
{
public static function message(Core\Db\DbError::Kind $kind): string
{
return match ($kind) {
Core\Db\DbError::Kind::UniqueViolation => 'that email address is already registered',
Core\Db\DbError::Kind::NotNullViolation => 'please fill in every field',
Core\Db\DbError::Kind::CheckViolation => 'one of those values is not allowed',
Core\Db\DbError::Kind::ForeignKeyViolation => 'the plan you picked is no longer available',
default => 'something went wrong on our side, please try again',
};
}
// The ones above are the person's to fix. Everything else is yours, so it
// goes to the log with the details while the screen stays plain.
public static function worthLogging(Core\Db\DbError::Kind $kind): bool
{
return SignUp::message($kind) == 'something went wrong on our side, please try again';
}
}
// What four attempts at the form came back with.
array<Core\Db\DbError::Kind> $outcomes = [
Core\Db\DbError::Kind::UniqueViolation,
Core\Db\DbError::Kind::NotNullViolation,
Core\Db\DbError::Kind::Syntax,
Core\Db\DbError::Kind::Permission,
];
foreach ($outcomes as Core\Db\DbError::Kind $kind) {
echo SignUp::message($kind),
SignUp::worthLogging($kind) ? ' (and logged for us)' : '',
"\n";
}
Output
that email address is already registered
please fill in every field
something went wrong on our side, please try again (and logged for us)
something went wrong on our side, please try again (and logged for us)

Trying again only when it is worth it

<?nvs
// Two of these refusals mean "nothing is wrong, do it again": the server
// picked your transaction to lose so that another one could finish. Every
// other refusal will fail the same way however many times you send it, so a
// retry there only makes the outage longer.
class Retry
{
public static function worthIt(Core\Db\DbError::Kind $kind): bool
{
return match ($kind) {
Core\Db\DbError::Kind::Deadlock => true,
Core\Db\DbError::Kind::SerializationFailure => true,
default => false,
};
}
}
// What a transfer between two accounts came back with, attempt by attempt,
// with three attempts allowed.
array<Core\Db\DbError::Kind> $attempts = [
Core\Db\DbError::Kind::Deadlock,
Core\Db\DbError::Kind::SerializationFailure,
Core\Db\DbError::Kind::UniqueViolation,
];
uint $attempt = 0;
foreach ($attempts as Core\Db\DbError::Kind $kind) {
$attempt = $attempt + 1;
if (Retry::worthIt($kind)) {
echo 'attempt ', $attempt, ": lost a race, sending it again\n";
} else {
// This one is the program's own mistake, so it is handed on rather
// than repeated. The transfer has already been rolled back.
echo 'attempt ', $attempt, ": that will not get better, giving up\n";
}
}
Output
attempt 1: lost a race, sending it again
attempt 2: lost a race, sending it again
attempt 3: that will not get better, giving up