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..42242bf0d11 100644 --- a/test/parallel/test-sqlite-template-tag.js +++ b/test/parallel/test-sqlite-template-tag.js @@ -298,22 +298,22 @@ test('sql.db returns the associated DatabaseSync instance', () => { 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)}`); - }, { + assert.throws(() => sql.get`SELECT nonexistent_column FROM foo`, { + name: 'Error', code: 'ERR_SQLITE_ERROR', - message: /no such column/i, + message: 'Failed to prepare statement', + errcode: 1, + errstr: 'SQL logic error', + errmsg: /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)}`); - }, { + assert.throws(() => sql.get`SELECT * FROM nonexistent_table`, { + name: 'Error', code: 'ERR_SQLITE_ERROR', - message: /no such table/i, + message: 'Failed to prepare statement', + errcode: 1, + errstr: 'SQL logic error', + errmsg: /no such table/i, }); }); @@ -384,7 +384,25 @@ test('cached statements are finalized when the database is closed', () => { db.open(); assert.throws(() => sql.all`SELECT id FROM foo`, { + name: 'Error', + code: 'ERR_SQLITE_ERROR', + message: 'Failed to prepare statement', + errcode: 1, + errstr: 'SQL logic error', + errmsg: 'no such table: foo' + }); +}); + +test('failed prepares throw', () => { + assert.throws(() => { + // eslint-disable-next-line no-unused-expressions + sql.all`SELECT * FROM does_not_exist`; + }, { + name: 'Error', + message: 'Failed to prepare statement', code: 'ERR_SQLITE_ERROR', - message: /no such table/i, + errcode: 1, + errstr: 'SQL logic error', + errmsg: 'no such table: does_not_exist' }); });