From f4e06eb29c783e00745e342f36cd8c4e79d378df Mon Sep 17 00:00:00 2001 From: Caelan Sayler Date: Fri, 15 Nov 2024 21:48:44 +0000 Subject: [PATCH] handle exceptions in sample and add more logging --- samples/CPlusPlusWidgets/main.cpp | 47 +++++++++++++++++++++---------- src/lib-cpp/src/statics.rs | 1 + src/lib-rust/src/manager.rs | 5 +++- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/samples/CPlusPlusWidgets/main.cpp b/samples/CPlusPlusWidgets/main.cpp index 6f1536ef..737e5c15 100644 --- a/samples/CPlusPlusWidgets/main.cpp +++ b/samples/CPlusPlusWidgets/main.cpp @@ -83,16 +83,20 @@ private: } downloaded = false; - updateInfo = updateManager->CheckForUpdates(); - if (updateInfo.has_value()) + try { - topText->SetLabel("Update Found: " + updateInfo.value().TargetFullRelease.Version); - } - else - { - topText->SetLabel("No Update Found."); + updateInfo = updateManager->CheckForUpdates(); + if (updateInfo.has_value()) + { + topText->SetLabel("Update Found: " + updateInfo.value().TargetFullRelease.Version); + } + else + { + topText->SetLabel("No Update Found."); + } } + catch (...) { /* exception will print in log */ } } void OnDownloadUpdates(wxCommandEvent& event) @@ -106,12 +110,16 @@ private: // start download on new thread std::thread([this]() { - updateManager->DownloadUpdates(updateInfo.value(), &MyFrame::HandleProgressCallbackStatic, this); - downloaded = true; - wxTheApp->CallAfter([this]() + try { - topText->SetLabel("Download Complete."); - }); + updateManager->DownloadUpdates(updateInfo.value(), &MyFrame::HandleProgressCallbackStatic, this); + downloaded = true; + wxTheApp->CallAfter([this]() + { + topText->SetLabel("Download Complete."); + }); + } + catch (...) { /* exception will print in log */ } }).detach(); } @@ -123,15 +131,24 @@ private: return; } - updateManager->WaitExitThenApplyUpdate(updateInfo.value()); - exit(0); + try + { + updateManager->WaitExitThenApplyUpdate(updateInfo.value()); + wxTheApp->ExitMainLoop(); + } + catch (...) { /* exception will print in log */ } + } void HandleVpkcLog(const char* pszLevel, const char* pszMessage) { std::string level(pszLevel); std::string message(pszMessage); - textArea->AppendText(level + ": " + message + "\n"); + wxTheApp->CallAfter([this, level, message]() { + if (textArea) { // Ensure textArea is valid. + textArea->AppendText(level + ": " + message + "\n"); + } + }); } static void HandleVpkcLogStatic(void* context, const char* pszLevel, const char* pszMessage) diff --git a/src/lib-cpp/src/statics.rs b/src/lib-cpp/src/statics.rs index 23ee8bc3..eb4b161b 100644 --- a/src/lib-cpp/src/statics.rs +++ b/src/lib-cpp/src/statics.rs @@ -59,6 +59,7 @@ where Ok(_) => true, Err(e) => { *last_error = format!("{:?}", e); + log::error!("{:?}", e); false } } diff --git a/src/lib-rust/src/manager.rs b/src/lib-rust/src/manager.rs index 817bb1db..9cbe5621 100644 --- a/src/lib-rust/src/manager.rs +++ b/src/lib-rust/src/manager.rs @@ -460,7 +460,10 @@ impl UpdateManager { let mut p = Process::new(&self.locator.get_update_path()); p.args(&args); - p.current_dir(&self.locator.get_root_dir()); + + if let Some(update_exe_parent) = self.locator.get_update_path().parent() { + p.current_dir(update_exe_parent); + } #[cfg(target_os = "windows")] {