diff --git a/backup.db b/backup.db index a1c75f32be3e58..e69de29bb2d1d6 100644 Binary files a/backup.db and b/backup.db differ diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index 23a5952742c0a0..9bc1179e21add4 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -85,6 +85,24 @@ inline MaybeLocal CreateSQLiteError(Isolate* isolate, return e; } +inline MaybeLocal CreateSQLiteError(Isolate* isolate, int errcode) { + const char* errstr = sqlite3_errstr(errcode); + Local js_errmsg; + Local e; + Environment* env = Environment::GetCurrent(isolate); + if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errmsg) || + !CreateSQLiteError(isolate, errstr).ToLocal(&e) || + e->Set(isolate->GetCurrentContext(), + env->errcode_string(), + Integer::New(isolate, errcode)) + .IsNothing() || + e->Set(isolate->GetCurrentContext(), env->errstr_string(), js_errmsg) + .IsNothing()) { + return MaybeLocal(); + } + return e; +} + inline MaybeLocal CreateSQLiteError(Isolate* isolate, sqlite3* db) { int errcode = sqlite3_extended_errcode(db); const char* errstr = sqlite3_errstr(errcode); @@ -192,6 +210,8 @@ class BackupJob : public ThreadPoolWork { void DoThreadPoolWork() override { backup_status_ = sqlite3_backup_step(pBackup_, pages_); + + const char* errstr = sqlite3_errstr(backup_status_); } void AfterThreadPoolWork(int status) override { @@ -209,11 +229,13 @@ class BackupJob : public ThreadPoolWork { if (!(backup_status_ == SQLITE_OK || backup_status_ == SQLITE_DONE || backup_status_ == SQLITE_BUSY || backup_status_ == SQLITE_LOCKED)) { Local e = Local(); - CreateSQLiteError(env()->isolate(), pDest_).ToLocal(&e); + CreateSQLiteError(env()->isolate(), backup_status_).ToLocal(&e); Cleanup(); resolver->Reject(env()->context(), e).ToChecked(); + + return; } int total_pages = sqlite3_backup_pagecount(pBackup_); @@ -252,17 +274,16 @@ class BackupJob : public ThreadPoolWork { String::NewFromUtf8( env()->isolate(), "Backup completed", NewStringType::kNormal) .ToLocalChecked(); - Local error_message = - String::NewFromUtf8( - env()->isolate(), "Could not finish backup", NewStringType::kNormal) - .ToLocalChecked(); + + Local e = Local(); + CreateSQLiteError(env()->isolate(), pDest_).ToLocal(&e); int dest_status = Cleanup(); if (dest_status == SQLITE_OK) { resolver->Resolve(env()->context(), message).ToChecked(); } else { - resolver->Reject(env()->context(), error_message).ToChecked(); + resolver->Reject(env()->context(), e).ToChecked(); } } @@ -809,17 +830,6 @@ void DatabaseSync::Backup(const FunctionCallbackInfo& args) { BackupJob* job = new BackupJob( env, db, resolver, source_db, *destFilename, dest_db, rate, progressFunc); job->ScheduleBackup(); - - /* if (job->backup_status_ != SQLITE_OK) { */ - /* Local message = */ - /* String::NewFromUtf8(env->isolate(), "Could not start backup") */ - /* .ToLocalChecked(); */ - /* Local error_message = */ - /* String::NewFromUtf8(env->isolate(), "Could not start backup") */ - /* .ToLocalChecked(); */ - /* resolver->Reject(env->context(), message).ToChecked(); */ - /* delete job; */ - /* } */ } void DatabaseSync::CustomFunction(const FunctionCallbackInfo& args) { diff --git a/test/parallel/test-sqlite-backup.mjs b/test/parallel/test-sqlite-backup.mjs index 2d9c0820f5be18..af4afbc4a844b9 100644 --- a/test/parallel/test-sqlite-backup.mjs +++ b/test/parallel/test-sqlite-backup.mjs @@ -16,7 +16,7 @@ test('throws exception when trying to start backup from a closed database', asyn })); }); -test('database backup', async (t) => { +test.only('database backup', async (t) => { const progressFn = t.mock.fn(); const database = new DatabaseSync(':memory:');