Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node:sqlite unique constraint exception parity #28289

Open
andykais opened this issue Feb 25, 2025 · 0 comments · May be fixed by #28431
Open

node:sqlite unique constraint exception parity #28289

andykais opened this issue Feb 25, 2025 · 0 comments · May be fixed by #28431
Labels
node:sqlite Issues related to the `node:sqlite` module

Comments

@andykais
Copy link

andykais commented Feb 25, 2025

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'
}
@littledivy littledivy added the node:sqlite Issues related to the `node:sqlite` module label Feb 25, 2025
c00kie17 added a commit to c00kie17/deno that referenced this issue Mar 8, 2025
@c00kie17 c00kie17 linked a pull request Mar 8, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
node:sqlite Issues related to the `node:sqlite` module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants