Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error handing #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"gypfile": true,
"dependencies": {
"@mapbox/node-pre-gyp": "^1.0.0",
"node-addon-api": "*",
"node-addon-api": "^7.0.0",
"node-gyp": "^9.3.0"
},
"binary": {
Expand Down
12 changes: 10 additions & 2 deletions src/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,21 @@ void Database::Schedule(Napi::Env env, duckdb::unique_ptr<Task> task) {

static void TaskExecuteCallback(napi_env e, void *data) {
auto holder = (TaskHolder *)data;
holder->task->DoWork();
try {
holder->task->DoWork();
} catch (const Napi::Error &e) {
holder->task->Handle(e);
}
}

static void TaskCompleteCallback(napi_env e, napi_status status, void *data) {
duckdb::unique_ptr<TaskHolder> holder((TaskHolder *)data);
holder->db->TaskComplete(e);
holder->task->DoCallback();
try {
holder->task->DoCallback();
} catch (const Napi::Error &e) {
holder->task->Handle(e);
}
}

void Database::TaskComplete(Napi::Env env) {
Expand Down
22 changes: 22 additions & 0 deletions src/duckdb_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ struct Task {
// Called on a worker thread (i.e., not the main event loop thread)
virtual void DoWork() = 0;

virtual void Handle(const Napi::Error &e) {
Napi::HandleScope scope(object.Env());

auto function = callback.Value();
if (!function.IsUndefined()) {

const Napi::Object &recv = object.Value();

Napi::Error value = e.Env().GetAndClearPendingException();

D_ASSERT(!e.Env().IsExceptionPending());
D_ASSERT(function.IsObject());

try {
const Napi::Object &object1 = value.Value();
function.MakeCallback(recv, {object1});
} catch (const std::exception &e) {
duckdb::Printer::Print(e.what());
}
}
}

// Called on the event loop thread after the work has been completed. By
// default, call the associated callback, if defined. If you're writing
// a Task that uses promises, override this method instead of Callback.
Expand Down
Loading