From 9f8582c8e74a7204cfe3e9e021cab4e7bc0a4e6b Mon Sep 17 00:00:00 2001 From: Mike Wellner Date: Thu, 31 Jul 2025 14:44:14 +0200 Subject: [PATCH] Map errors to Napi::Errors --- src/main.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2550126..e3d48e6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,8 +15,18 @@ static Napi::Object getDiskUsage(const Napi::CallbackInfo& info) { Napi::String path = info[0].As(); std::string pathText = path; - - DiskUsage usage = GetDiskUsage(pathText.c_str()); + + DiskUsage usage = {}; + try { + usage = GetDiskUsage(pathText.c_str()); + } catch (const std::runtime_error& e) { + throw Napi::Error::New(env, std::string("GetDiskUsage failed: ") + e.what()); + } catch (const SystemError& e) { + std::string errorMessage = "SystemError: " + e.message() + " (syscall: " + e.syscall() + ", errno: " + std::to_string(e.errorno()) + ", path: " + e.path() + ")"; + throw Napi::Error::New(env, errorMessage); + } catch (...) { + throw Napi::Error::New(env, "An unknown error occurred while getting disk usage"); + } Napi::Object result = Napi::Object::New(env); result.Set("available", Napi::Number::New(env, static_cast(usage.available)));