Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
sqlite: improve error reporting for prepared statements in SQLTagStore
Fixes: #60198
  • Loading branch information
islandryu committed Oct 12, 2025
commit 71a8a57d581696060816c74976b2f3d9cacf856e
1 change: 1 addition & 0 deletions src/env_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
V(errno_string, "errno") \
V(error_string, "error") \
V(errstr_string, "errstr") \
V(errmsg_string, "errmsg") \
V(events_waiting, "eventsWaiting") \
V(events, "events") \
V(exclusive_string, "exclusive") \
Expand Down
46 changes: 45 additions & 1 deletion src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,35 @@ inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate, sqlite3* db) {
return e;
}

inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate,
sqlite3* db,
const char* message) {
int errcode = sqlite3_extended_errcode(db);
const char* errstr = sqlite3_errstr(errcode);
const char* errmsg = sqlite3_errmsg(db);
Local<String> js_errstr;
Local<String> js_errmsg;
Local<Object> e;
if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errmsg) ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to be swapped.

Suggested change
if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errmsg) ||
if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errstr) ||

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, that was my mistake.

!String::NewFromUtf8(isolate, errmsg).ToLocal(&js_errstr) ||
!CreateSQLiteError(isolate, message).ToLocal(&e) ||
e->Set(isolate->GetCurrentContext(),
Environment::GetCurrent(isolate)->errcode_string(),
Integer::New(isolate, errcode))
.IsNothing() ||
e->Set(isolate->GetCurrentContext(),
Environment::GetCurrent(isolate)->errstr_string(),
js_errstr)
.IsNothing() ||
e->Set(isolate->GetCurrentContext(),
Environment::GetCurrent(isolate)->errmsg_string(),
js_errmsg)
.IsNothing()) {
return MaybeLocal<Object>();
}
return e;
}

void JSValueToSQLiteResult(Isolate* isolate,
sqlite3_context* ctx,
Local<Value> value) {
Expand Down Expand Up @@ -241,6 +270,20 @@ inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, const char* message) {
}
}

inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate,
DatabaseSync* db,
const char* message) {
if (db->ShouldIgnoreSQLiteError()) {
db->SetIgnoreNextSQLiteError(false);
return;
}

Local<Object> e;
if (CreateSQLiteError(isolate, db->Connection(), message).ToLocal(&e)) {
isolate->ThrowException(e);
}
}

inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, int errcode) {
const char* errstr = sqlite3_errstr(errcode);

Expand Down Expand Up @@ -2906,7 +2949,8 @@ 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(), "Failed to prepare statement");
sqlite3_finalize(s);
return BaseObjectPtr<StatementSync>();
}
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-sqlite-template-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,16 @@ test('TagStore capacity, size, and clear', () => {
test('sql.db returns the associated DatabaseSync instance', () => {
assert.strictEqual(sql.db, db);
});

test('failed prepares throw', () => {
assert.throws(() => {
sql.all`SELECT * FROM does_not_exist`; // eslint-disable-line no-unused-expressions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the ESLint configuration.

}, {
name: 'Error',
message: 'Failed to prepare statement',
code: 'ERR_SQLITE_ERROR',
errcode: 1,
errstr: 'no such table: does_not_exist',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong way around I think, see other comment.

errmsg: 'SQL logic error'
});
});
Loading