From cb8bc3466d1101424aef553afa437395992f0010 Mon Sep 17 00:00:00 2001 From: Pramit Sharma Date: Sat, 27 Dec 2025 05:34:05 +0530 Subject: [PATCH] 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 Reviewed-By: Colin Ihrig Reviewed-By: Edy Silva Reviewed-By: Luigi Pinca --- src/node_sqlite.cc | 2 +- test/parallel/test-sqlite-template-tag.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index 5e035d64b87..6d35236dce0 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -2946,7 +2946,7 @@ BaseObjectPtr 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(); } diff --git a/test/parallel/test-sqlite-template-tag.js b/test/parallel/test-sqlite-template-tag.js index 627de70392c..f640e70f8c3 100644 --- a/test/parallel/test-sqlite-template-tag.js +++ b/test/parallel/test-sqlite-template-tag.js @@ -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, + }); +});