mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
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:
@@ -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>();
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user