Skip to content

Commit 9c346d2

Browse files
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: #61051 PR-URL: #61096 Reviewed-By: Zeyu "Alex" Yang <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Edy Silva <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 7786470 commit 9c346d2

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/node_sqlite.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2946,7 +2946,7 @@ BaseObjectPtr<StatementSync> SQLTagStore::PrepareStatement(
29462946
session->database_->connection_, sql.data(), sql.size(), &s, 0);
29472947

29482948
if (r != SQLITE_OK) {
2949-
THROW_ERR_SQLITE_ERROR(isolate, "Failed to prepare statement");
2949+
THROW_ERR_SQLITE_ERROR(isolate, session->database_.get());
29502950
sqlite3_finalize(s);
29512951
return BaseObjectPtr<StatementSync>();
29522952
}

test/parallel/test-sqlite-template-tag.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,25 @@ test('TagStore capacity, size, and clear', () => {
102102
test('sql.db returns the associated DatabaseSync instance', () => {
103103
assert.strictEqual(sql.db, db);
104104
});
105+
106+
test('sql error messages are descriptive', () => {
107+
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'test'})`.changes, 1);
108+
109+
// Test with non-existent column
110+
assert.throws(() => {
111+
const result = sql.get`SELECT nonexistent_column FROM foo`;
112+
assert.fail(`Expected error, got: ${JSON.stringify(result)}`);
113+
}, {
114+
code: 'ERR_SQLITE_ERROR',
115+
message: /no such column/i,
116+
});
117+
118+
// Test with non-existent table
119+
assert.throws(() => {
120+
const result = sql.get`SELECT * FROM nonexistent_table`;
121+
assert.fail(`Expected error, got: ${JSON.stringify(result)}`);
122+
}, {
123+
code: 'ERR_SQLITE_ERROR',
124+
message: /no such table/i,
125+
});
126+
});

0 commit comments

Comments
 (0)