Function isUniqueViolation

Whether error is a unique or primary-key violation, from any adapter: PostgreSQL's SQLSTATE 23505 (unique_violation, pg's code), better-sqlite3's SQLITE_CONSTRAINT_UNIQUE and SQLITE_CONSTRAINT_PRIMARYKEY, and the "UNIQUE constraint failed" message that sql.js, the IndexedDB adapter and the Capacitor plugin give. An error that wraps one as its cause, to any depth up to ten, counts too.

try {
await db.run('INSERT INTO users (email) VALUES (?)', [email]);
} catch (error) {
if (isUniqueViolation(error)) return { taken: true };
throw error;
}