farion1231/cc-switch · error · Error

${error}

Error message

${error}

What it means

setGlobalProxyUrl forwards the URL to the Tauri command set_global_proxy_url and re-wraps any rejection as an Error, normalizing the string payloads Tauri invoke produces (typeof error === 'string' ? error : String(error)). The message text therefore comes from the Rust backend, not from this file — typical causes are a malformed/unsupported proxy URL or an OS-level failure while applying the system proxy.

Solutions

  1. Normalize input to a full URL with scheme (http://127.0.0.1:7890 or socks5://127.0.0.1:1080) before calling
  2. Pre-validate with new URL(url) plus a scheme allow-list in the UI; use testProxyUrl to confirm reachability first
  3. Inspect the Rust set_global_proxy_url command's error strings and map them to actionable UI messages

Example fix

// before
await setGlobalProxyUrl('127.0.0.1:7890'); // scheme-less URL rejected by backend

// after
const u = new URL('http://127.0.0.1:7890'); // build with explicit scheme
await setGlobalProxyUrl(u.toString());
Defensive patterns

Strategy: try-catch

Validate before calling

function normalizeProxyUrl(input: string): string | null {
  try {
    const u = new URL(input.trim());
    return ['http:', 'https:', 'socks5:'].includes(u.protocol) ? u.toString() : null;
  } catch {
    return null;
  }
}
const normalized = normalizeProxyUrl(raw);
if (!normalized) {
  // show a field error; do not call setGlobalProxyUrl
}

Try / catch

try {
  await setGlobalProxyUrl(url);
} catch (error) {
  // message is the backend string normalized to Error by the wrapper
  showProxyError(error instanceof Error ? error.message : String(error));
}

Prevention

When it happens

Trigger: invoke('set_global_proxy_url', { url }) rejects — e.g. url '127.0.0.1:7890' without a scheme, an unsupported scheme, a URL the backend cannot parse, or the OS refusing the proxy change.

Common situations: User pastes host:port without an http:// or socks5:// prefix; proxy credentials/port typed into the URL field; security software or permissions blocking system proxy changes.

Related errors


AI-assisted analysis of farion1231/cc-switch@0b5da51016 (2026-08-20). Data as JSON: /api/errors/7e70ad0e3b46af20. Report an issue: GitHub.

Appendix: source

Thrown at src/lib/api/globalProxy.ts:55

 *
 * @returns 代理 URL,null 表示未配置(直连)
 */
export async function getGlobalProxyUrl(): Promise<string | null> {
  return invoke<string | null>("get_global_proxy_url");
}

/**
 * 设置全局代理 URL
 *
 * @param url - 代理 URL(如 http://127.0.0.1:7890 或 socks5://127.0.0.1:1080)
 *              空字符串表示清除代理(直连)
 */
export async function setGlobalProxyUrl(url: string): Promise<void> {
  try {
    return await invoke("set_global_proxy_url", { url });
  } catch (error) {
    // Tauri invoke 错误可能是字符串
    throw new Error(typeof error === "string" ? error : String(error));
  }
}

/**
 * 测试代理连接
 *
 * @param url - 要测试的代理 URL
 * @returns 测试结果,包含是否成功、延迟和错误信息
 */
export async function testProxyUrl(url: string): Promise<ProxyTestResult> {
  return invoke<ProxyTestResult>("test_proxy_url", { url });
}

/**
 * 获取当前出站代理状态
 *
 * @returns 代理状态,包含是否启用和代理 URL
 */

View on GitHub (pinned to 0b5da51016)