Skip to content

Commit c2d2cfb

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
1 parent b1c01fc commit c2d2cfb

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/node_sqlite.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ class CustomAggregate {
309309
static inline void xStepBase(sqlite3_context* ctx,
310310
int argc,
311311
sqlite3_value** argv,
312-
Global<Function> CustomAggregate::*mptr) {
312+
Global<Function> CustomAggregate::* mptr) {
313313
CustomAggregate* self =
314314
static_cast<CustomAggregate*>(sqlite3_user_data(ctx));
315315
Environment* env = self->env_;
@@ -2947,7 +2947,7 @@ BaseObjectPtr<StatementSync> SQLTagStore::PrepareStatement(
29472947
session->database_->connection_, sql.data(), sql.size(), &s, 0);
29482948

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

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)