diff --git a/eslint.config.mjs b/eslint.config.mjs index c3c276fd80f..d38969c6179 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -238,7 +238,7 @@ export default [ 'no-throw-literal': 'error', 'no-undef': ['error', { typeof: true }], 'no-undef-init': 'error', - 'no-unused-expressions': ['error', { allowShortCircuit: true }], + 'no-unused-expressions': ['error', { allowShortCircuit: true, allowTaggedTemplates: true }], 'no-unused-vars': ['error', { args: 'none', caughtErrors: 'all' }], 'no-use-before-define': ['error', { classes: true, diff --git a/src/env_properties.h b/src/env_properties.h index eb26d3b6cf0..c04656b7284 100644 --- a/src/env_properties.h +++ b/src/env_properties.h @@ -178,6 +178,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") \ diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index c6b8a8d0a61..9698ad741da 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -290,6 +290,35 @@ inline MaybeLocal CreateSQLiteError(Isolate* isolate, sqlite3* db) { isolate, sqlite3_errmsg(db), sqlite3_errstr(errcode), errcode); } +inline MaybeLocal 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 js_errstr; + Local js_errmsg; + Local e; + if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errstr) || + !String::NewFromUtf8(isolate, errmsg).ToLocal(&js_errmsg) || + !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(); + } + return e; +} + void JSValueToSQLiteResult(Isolate* isolate, sqlite3_context* ctx, Local value) { @@ -344,6 +373,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 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); @@ -3972,7 +4015,9 @@ BaseObjectPtr SQLTagStore::PrepareStatement( StatementPtr stmt_ptr(s); if (r != SQLITE_OK) { - THROW_ERR_SQLITE_ERROR(isolate, session->database_.get()); + THROW_ERR_SQLITE_ERROR( + isolate, session->database_.get(), "Failed to prepare statement"); + sqlite3_finalize(s); return BaseObjectPtr(); } diff --git a/test/parallel/test-sqlite-template-tag.js b/test/parallel/test-sqlite-template-tag.js index eaa6d19fc7c..f17701b409a 100644 --- a/test/parallel/test-sqlite-template-tag.js +++ b/test/parallel/test-sqlite-template-tag.js @@ -388,3 +388,16 @@ test('cached statements are finalized when the database is closed', () => { message: /no such table/i, }); }); + +test('failed prepares throw', () => { + assert.throws(() => { + sql.all`SELECT * FROM does_not_exist`; + }, { + name: 'Error', + message: 'Failed to prepare statement', + code: 'ERR_SQLITE_ERROR', + errcode: 1, + errstr: 'SQL logic error', + errmsg: 'no such table: does_not_exist' + }); +});