Skip to content

node:sqlite unique constraint exception parity #28289

Closed
@andykais

Description

@andykais

Version: Deno 2.2.1

When node:sqlite throws an exception from deno, we get an Error with a message about unique constraints. When used in node v23, it throws an Error with a code, errcode, and errstr that all make detecting a unique constraint issue much easier. This is important when building applications have database constraints, since a lot of these kinds of unique constraints need to be bubbled up to the user. This is especially problematic when a unique constraint error happens from a prepared statement in deno. In that case, we dont actually have any error string information we can use to tell if the exception was a unique constraint

deno error output:

Deno 2.2.1
exit using ctrl+d, ctrl+c, or close()
REPL is running with all permissions allowed.
To specify permissions, run `deno repl` with allow flags.
> import * as sqlite from 'node:sqlite'
undefined
> let db = new sqlite.DatabaseSync(':memory:')
undefined
> db.exec('CREATE TABLE person ( name TEXT NOT NULL UNIQUE );')
null
> db.exec(`INSERT INTO person (name) VALUES ('johnny')`)
null
> db.exec(`INSERT INTO person (name) VALUES ('johnny')`)
Uncaught Error: UNIQUE constraint failed: person.name
    at <anonymous>:1:25
> db.prepare(`INSERT INTO person (name) VALUES ('johnny')`).run()
Uncaught Error: Failed to step statement
    at <anonymous>:1:80
>

node error output:

Welcome to Node.js v23.8.0.
Type ".help" for more information.
> const sqlite = require('node:sqlite')
undefined
> (node:44980) ExperimentalWarning: SQLite is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
> db = new sqlite.DatabaseSync(':memory:')
DatabaseSync {}
> db.exec('CREATE TABLE person ( name TEXT NOT NULL UNIQUE );')
undefined
> db.exec(`INSERT INTO person (name) VALUES ('johnny')`)
undefined
> db.exec(`INSERT INTO person (name) VALUES ('johnny')`)
Uncaught [Error: UNIQUE constraint failed: person.name] {
  code: 'ERR_SQLITE_ERROR',
  errcode: 2067,
  errstr: 'constraint failed'
}
> db.prepare(`INSERT INTO person (name) VALUES ('johnny')`).run()
Uncaught [Error: UNIQUE constraint failed: person.name] {
  code: 'ERR_SQLITE_ERROR',
  errcode: 2067,
  errstr: 'constraint failed'
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    node:sqliteIssues related to the `node:sqlite` module

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions