From cfcd7b892ae88810770ca9731cfdb5158b586c42 Mon Sep 17 00:00:00 2001 From: Jason Date: Sat, 8 Nov 2025 22:55:53 +0800 Subject: [PATCH] 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. --- src-tauri/src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index a736b0d..2440eb8 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -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); // 执行切换