From 07136b73f03d996d1319c82f852ef20d6bbd7a9d Mon Sep 17 00:00:00 2001 From: Elliana May Date: Sun, 1 Oct 2023 22:38:44 +0800 Subject: [PATCH] improve error handing --- package-lock.json | 14 +++++++------- package.json | 2 +- src/database.cpp | 12 ++++++++++-- src/duckdb_node.hpp | 22 ++++++++++++++++++++++ 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5e05e4d1..04d0e803 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "license": "MIT", "dependencies": { "@mapbox/node-pre-gyp": "^1.0.0", - "node-addon-api": "*", + "node-addon-api": "^7.0.0", "node-gyp": "^9.3.0" }, "devDependencies": { @@ -2110,9 +2110,9 @@ } }, "node_modules/node-addon-api": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-4.2.0.tgz", - "integrity": "sha512-eazsqzwG2lskuzBqCGPi7Ac2UgOoMz8JVOXVhTvvPDYhthvNpefx8jWD8Np7Gv+2Sz0FlPWZk0nJV0z598Wn8Q==" + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-7.0.0.tgz", + "integrity": "sha512-vgbBJTS4m5/KkE16t5Ly0WW9hz46swAstv0hYYwMtbG7AznRhNyfLRe8HZAiWIpcHzoO7HxhLuBQj9rJ/Ho0ZA==" }, "node_modules/node-fetch": { "version": "2.6.6", @@ -4768,9 +4768,9 @@ "integrity": "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==" }, "node-addon-api": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-4.2.0.tgz", - "integrity": "sha512-eazsqzwG2lskuzBqCGPi7Ac2UgOoMz8JVOXVhTvvPDYhthvNpefx8jWD8Np7Gv+2Sz0FlPWZk0nJV0z598Wn8Q==" + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-7.0.0.tgz", + "integrity": "sha512-vgbBJTS4m5/KkE16t5Ly0WW9hz46swAstv0hYYwMtbG7AznRhNyfLRe8HZAiWIpcHzoO7HxhLuBQj9rJ/Ho0ZA==" }, "node-fetch": { "version": "2.6.6", diff --git a/package.json b/package.json index ba5c1e92..bbe1ba42 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/database.cpp b/src/database.cpp index 5f052e55..a5fa944c 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -130,13 +130,21 @@ void Database::Schedule(Napi::Env env, duckdb::unique_ptr 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 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) { diff --git a/src/duckdb_node.hpp b/src/duckdb_node.hpp index e3eea68b..b6e69b71 100644 --- a/src/duckdb_node.hpp +++ b/src/duckdb_node.hpp @@ -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.