Skip to content

Commit

Permalink
wip: add new helper for creating errors
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksilva97 committed Jan 12, 2025
1 parent 0b38fba commit 5c67faa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
Binary file modified backup.db
Binary file not shown.
44 changes: 27 additions & 17 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate,
return e;
}

inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate, int errcode) {
const char* errstr = sqlite3_errstr(errcode);
Local<String> js_errmsg;
Local<Object> 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<Object>();
}
return e;
}

inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate, sqlite3* db) {
int errcode = sqlite3_extended_errcode(db);
const char* errstr = sqlite3_errstr(errcode);
Expand Down Expand Up @@ -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 {
Expand All @@ -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<Object> e = Local<Object>();

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_);
Expand Down Expand Up @@ -252,17 +274,16 @@ class BackupJob : public ThreadPoolWork {
String::NewFromUtf8(
env()->isolate(), "Backup completed", NewStringType::kNormal)
.ToLocalChecked();
Local<String> error_message =
String::NewFromUtf8(
env()->isolate(), "Could not finish backup", NewStringType::kNormal)
.ToLocalChecked();

Local<Object> e = Local<Object>();
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();
}
}

Expand Down Expand Up @@ -809,17 +830,6 @@ void DatabaseSync::Backup(const FunctionCallbackInfo<Value>& args) {
BackupJob* job = new BackupJob(
env, db, resolver, source_db, *destFilename, dest_db, rate, progressFunc);
job->ScheduleBackup();

/* if (job->backup_status_ != SQLITE_OK) { */
/* Local<String> message = */
/* String::NewFromUtf8(env->isolate(), "Could not start backup") */
/* .ToLocalChecked(); */
/* Local<String> error_message = */
/* String::NewFromUtf8(env->isolate(), "Could not start backup") */
/* .ToLocalChecked(); */
/* resolver->Reject(env->context(), message).ToChecked(); */
/* delete job; */
/* } */
}

void DatabaseSync::CustomFunction(const FunctionCallbackInfo<Value>& args) {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-sqlite-backup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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:');

Expand Down

0 comments on commit 5c67faa

Please sign in to comment.