Reference
Error helpers
Cross-dialect constraint detection and normalized database error info exported from better-drizzle.
better-drizzle exports helpers that normalize database errors across PostgreSQL, SQLite, and MySQL, so you can classify failures without string-matching driver messages. See error handling for end-to-end usage.
import {
isDatabaseError,
getDatabaseErrorInfo,
isUniqueViolation,
isForeignKeyViolation,
isNotNullViolation,
isCheckViolation,
} from 'better-drizzle';Predicates
Each returns a boolean and accepts an optional constraint name (or column name, for isNotNullViolation) to match a specific one.
| Function | Signature |
|---|---|
isDatabaseError | (error: unknown) => boolean |
isUniqueViolation | (error: unknown, constraint?: string) => boolean |
isForeignKeyViolation | (error: unknown, constraint?: string) => boolean |
isNotNullViolation | (error: unknown, column?: string) => boolean |
isCheckViolation | (error: unknown, constraint?: string) => boolean |
try {
await client.users.create({ data });
} catch (error) {
if (isUniqueViolation(error, 'users_email_unique')) {
// handle duplicate email specifically
}
throw error;
}getDatabaseErrorInfo
Parses any caught error into a normalized object, regardless of driver:
const info = getDatabaseErrorInfo(error);Prop
Type
BetterDrizzleTransactionRollbackError
Thrown when a transaction is explicitly rolled back via tx.rollback(reason). The reason you pass is available on the error:
import { BetterDrizzleTransactionRollbackError } from 'better-drizzle';
try {
await client.transaction(async (tx) => {
tx.rollback('duplicate email');
});
} catch (error) {
if (error instanceof BetterDrizzleTransactionRollbackError) {
console.log(error.reason); // 'duplicate email'
}
}Lock-related error codes
Locked reads throw normal BetterDrizzleError instances with specific codes:
| Code | Meaning |
|---|---|
LOCK_NOT_SUPPORTED | The dialect or query shape does not support the requested row lock. |
LOCK_REQUIRES_TRANSACTION | The client was configured with locks.transactionsOnly: true and the read ran outside a transaction. |
LOCK_TIMEOUT | The database could not acquire the requested lock in time, including NOWAIT failures. |