Skip to content

Commit f23eb73

Browse files
authored
fix: add a panic hook to collect logs and show a dialog (#191)
1 parent 3c48bb6 commit f23eb73

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

backend/tauri/src/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ fn main() -> std::io::Result<()> {
2222

2323
crate::log_err!(init::init_config());
2424

25+
// Panic Hook to show a panic dialog and save logs
26+
let default_panic = std::panic::take_hook();
27+
std::panic::set_hook(Box::new(move |info| {
28+
error!(format!("panic hook: {:?}", info));
29+
utils::dialog::panic_dialog(&format!("{:?}", info));
30+
default_panic(info);
31+
}));
32+
2533
#[allow(unused_mut)]
2634
let mut builder = tauri::Builder::default()
2735
.system_tray(SystemTray::new())

backend/tauri/src/utils/dialog.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::sync::{Arc, Barrier};
2+
use tauri::api::dialog::{MessageDialogBuilder, MessageDialogButtons, MessageDialogKind};
3+
4+
pub fn panic_dialog(msg: &str) {
5+
let msg = format!(
6+
"{}\n\nPlease report this issue to Github issue tracker.",
7+
msg
8+
);
9+
let barrier = Arc::new(Barrier::new(2));
10+
let barrier_ref = barrier.clone();
11+
MessageDialogBuilder::new("Error", msg)
12+
.kind(MessageDialogKind::Error)
13+
.buttons(MessageDialogButtons::Ok)
14+
.show(move |_| {
15+
barrier_ref.wait();
16+
});
17+
barrier.wait();
18+
}

backend/tauri/src/utils/dirs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static APP_DIR: &str = "clash-verge-dev";
1313
static CLASH_CONFIG: &str = "config.yaml";
1414
static VERGE_CONFIG: &str = "verge.yaml";
1515
static PROFILE_YAML: &str = "profiles.yaml";
16-
static STORAGE_DB: &str = "storage.db";
16+
static STORAGE_DB: &str = "storage";
1717

1818
static mut RESOURCE_DIR: Option<PathBuf> = None;
1919

backend/tauri/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod candy;
2+
pub mod dialog;
23
pub mod dirs;
34
pub mod help;
45
pub mod init;

0 commit comments

Comments
 (0)