diff --git a/src-tauri/src/cmd/clash.rs b/src-tauri/src/cmd/clash.rs index 803889dc5..47611d847 100644 --- a/src-tauri/src/cmd/clash.rs +++ b/src-tauri/src/cmd/clash.rs @@ -18,8 +18,7 @@ use tokio::fs; #[tauri::command] #[specta::specta] pub async fn copy_clash_env() -> CmdResult { - feat::copy_clash_env().await; - Ok(()) + feat::copy_clash_env().await.stringify_err() } #[tauri::command] diff --git a/src-tauri/src/core/tray/mod.rs b/src-tauri/src/core/tray/mod.rs index 326f51fa0..3d7f0aaf5 100644 --- a/src-tauri/src/core/tray/mod.rs +++ b/src-tauri/src/core/tray/mod.rs @@ -948,7 +948,15 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) { logging!(error, Type::Tray, "Failed to close all connections from tray: {err}"); } } - MenuIds::COPY_ENV => feat::copy_clash_env().await, + MenuIds::COPY_ENV => { + if let Err(err) = feat::copy_clash_env().await { + logging!( + error, + Type::Tray, + "Failed to copy proxy environment variables from tray: {err:#}" + ); + } + } MenuIds::CONF_DIR => { let _ = cmd::open_app_dir().await; } diff --git a/src-tauri/src/feat/proxy.rs b/src-tauri/src/feat/proxy.rs index 49eb4ae9f..80cc2c667 100644 --- a/src-tauri/src/feat/proxy.rs +++ b/src-tauri/src/feat/proxy.rs @@ -11,6 +11,7 @@ use crate::{ window_manager::WindowManager, }, }; +use anyhow::Context as _; use clash_verge_logging::{Type, logging}; use std::env; use tauri_plugin_clipboard_manager::ClipboardExt as _; @@ -95,7 +96,7 @@ pub async fn toggle_tun_mode(not_save_file: Option) -> bool { } } -pub async fn copy_clash_env() { +pub async fn copy_clash_env() -> anyhow::Result<()> { let env_ip = env::var("CLASH_VERGE_REV_IP").ok(); let verge_cfg = Config::verge().await.latest_arc(); let ip = env_ip @@ -133,12 +134,11 @@ pub async fn copy_clash_env() { } "fish" => format!("set -x http_proxy {http_proxy}; set -x https_proxy {http_proxy}"), _ => { - logging!(error, Type::ProxyMode, "copy_clash_env: Invalid env type! {env_type}"); - return; + anyhow::bail!("Invalid environment variable format: {env_type}"); } }; - if let Err(err) = clipboard.write_text(&export_text) { - logging!(error, Type::ProxyMode, "Failed to write to clipboard: {err}"); - } + clipboard + .write_text(export_text) + .context("Failed to write proxy environment variables to clipboard") } diff --git a/src/components/setting/setting-verge-basic.tsx b/src/components/setting/setting-verge-basic.tsx index f1e4082ac..89e8b6825 100644 --- a/src/components/setting/setting-verge-basic.tsx +++ b/src/components/setting/setting-verge-basic.tsx @@ -9,6 +9,7 @@ import { useVerge } from '@/hooks/use-verge' import { navigationItems } from '@/pages/_navigation-meta' import { copyClashEnv } from '@/services/cmds' import { supportedLanguages } from '@/services/i18n' +import { mutate } from '@/services/mutate' import { showNotice } from '@/services/notice-service' import getSystem from '@/utils/get-system' @@ -74,8 +75,18 @@ const SettingVergeBasic = ({ onError }: Props) => { } const onCopyClashEnv = useCallback(async () => { - await copyClashEnv() - showNotice.success('shared.feedback.notifications.common.copySuccess', 1000) + try { + await mutate(copyClashEnv, { + id: 'copy-clash-env', + onFulfilled: () => + showNotice.success( + 'shared.feedback.notifications.common.copySuccess', + 1000, + ), + }) + } catch { + // The shared mutation boundary reports the failure. + } }, []) return (