Skip to content

Commit b380a78

Browse files
committed
feat: optimize main function; proper error handling
1 parent 73282e5 commit b380a78

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ fn main() {
1212
env_logger::init();
1313

1414
// start tray service
15-
start_tray_service();
15+
match start_tray_service() {
16+
Ok(_) => println!("Tray service started successfully."),
17+
Err(e) => eprintln!("Failed to start the tray service: {}", e),
18+
}
1619

1720
// keep the main thread alive
18-
loop {
19-
park();
20-
}
21+
park();
2122
}

src/tray/utils.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use crate::tailscale;
22
use crate::tray::menu::SysTray;
3+
use std::error::Error;
34

4-
pub fn start_tray_service() {
5-
// start the tray service
6-
let _handle = ksni::spawn(SysTray {
7-
ctx: tailscale::status::get_current_status()
8-
.expect("Failed to update Tailscale status! Is Tailscale daemon running?"),
9-
})
10-
.unwrap_or_else(|e| {
11-
panic!("Failed to start the tray service: {}", e);
12-
});
5+
type TrayServiceError = Box<dyn Error>;
6+
7+
pub fn start_tray_service() -> Result<(), TrayServiceError> {
8+
let status = tailscale::status::get_current_status()
9+
.map_err(|e| format!("Failed to update Tailscale status: {}", e))?;
10+
11+
let _handle = ksni::spawn(SysTray { ctx: status })
12+
.map_err(|e| format!("Failed to spawn Tray implementation: {}", e))?;
13+
14+
Ok(())
1315
}

0 commit comments

Comments
 (0)