sqlite: improve error messages for tag store

When using SQLite tag store functions (sql.get`, sql.run`, etc.),
preparation errors now show descriptive SQLite error messages instead
of the generic 'Failed to prepare statement' message.

The tag store's GetOrCreateStatement function now passes the database
object to THROW_ERR_SQLITE_ERROR, allowing it to retrieve the actual
error message from sqlite3_errmsg(), matching the behavior of the
regular db.prepare() method.

Fixes: https://github.com/nodejs/node/issues/61051
PR-URL: https://github.com/nodejs/node/pull/61096
Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Pramit Sharma
2025-12-27 05:34:05 +05:30
committed by GitHub
parent 7786470ff5
commit 9c346d22a7
2 changed files with 23 additions and 1 deletions

View File

@@ -2946,7 +2946,7 @@ BaseObjectPtr<StatementSync> SQLTagStore::PrepareStatement(
session->database_->connection_, sql.data(), sql.size(), &s, 0);
if (r != SQLITE_OK) {
THROW_ERR_SQLITE_ERROR(isolate, "Failed to prepare statement");
THROW_ERR_SQLITE_ERROR(isolate, session->database_.get());
sqlite3_finalize(s);
return BaseObjectPtr<StatementSync>();
}

View File

@@ -102,3 +102,25 @@ test('TagStore capacity, size, and clear', () => {
test('sql.db returns the associated DatabaseSync instance', () => {
assert.strictEqual(sql.db, db);
});
test('sql error messages are descriptive', () => {
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'test'})`.changes, 1);
// Test with non-existent column
assert.throws(() => {
const result = sql.get`SELECT nonexistent_column FROM foo`;
assert.fail(`Expected error, got: ${JSON.stringify(result)}`);
}, {
code: 'ERR_SQLITE_ERROR',
message: /no such column/i,
});
// Test with non-existent table
assert.throws(() => {
const result = sql.get`SELECT * FROM nonexistent_table`;
assert.fail(`Expected error, got: ${JSON.stringify(result)}`);
}, {
code: 'ERR_SQLITE_ERROR',
message: /no such table/i,
});
});