fix(tray): replace unwrap with safe pattern matching in menu handler

Replace unwrap() calls with safe pattern matching to prevent panics
when handling invalid tray menu item IDs. Now logs errors and returns
gracefully instead of crashing the application.
This commit is contained in:
Jason
2025-11-08 22:55:53 +08:00
parent 3da787b9af
commit cfcd7b892a

View File

@@ -243,7 +243,10 @@ fn handle_tray_menu_event(app: &tauri::AppHandle, event_id: &str) {
app.exit(0);
}
id if id.starts_with("claude_") => {
let provider_id = id.strip_prefix("claude_").unwrap();
let Some(provider_id) = id.strip_prefix("claude_") else {
log::error!("无效的 Claude 菜单项 ID: {}", id);
return;
};
log::info!("切换到Claude供应商: {}", provider_id);
// 执行切换
@@ -260,7 +263,10 @@ fn handle_tray_menu_event(app: &tauri::AppHandle, event_id: &str) {
});
}
id if id.starts_with("codex_") => {
let provider_id = id.strip_prefix("codex_").unwrap();
let Some(provider_id) = id.strip_prefix("codex_") else {
log::error!("无效的 Codex 菜单项 ID: {}", id);
return;
};
log::info!("切换到Codex供应商: {}", provider_id);
// 执行切换