|
| 1 | +import * as vscode from "vscode" |
| 2 | + |
| 3 | +/** |
| 4 | + * Detects if the user has proxy settings configured but http.electronFetch is disabled. |
| 5 | + * This combination can cause connection errors with certain API providers. |
| 6 | + */ |
| 7 | +export function detectProxyConfigurationIssue(): { |
| 8 | + hasProxyConfig: boolean |
| 9 | + electronFetchEnabled: boolean |
| 10 | + hasIssue: boolean |
| 11 | + proxySettings: string[] |
| 12 | +} { |
| 13 | + const config = vscode.workspace.getConfiguration() |
| 14 | + |
| 15 | + // Check for proxy-related settings |
| 16 | + const httpProxy = config.get<string>("http.proxy") |
| 17 | + const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy |
| 18 | + const httpProxyEnv = process.env.HTTP_PROXY || process.env.http_proxy |
| 19 | + const allProxy = process.env.ALL_PROXY || process.env.all_proxy |
| 20 | + |
| 21 | + // Check if electronFetch is enabled (default is false in VSCode) |
| 22 | + const electronFetchEnabled = config.get<boolean>("http.electronFetch", false) |
| 23 | + |
| 24 | + // Collect all proxy settings found |
| 25 | + const proxySettings: string[] = [] |
| 26 | + if (httpProxy) proxySettings.push(`VSCode http.proxy: ${httpProxy}`) |
| 27 | + if (httpsProxy) proxySettings.push(`HTTPS_PROXY: ${httpsProxy}`) |
| 28 | + if (httpProxyEnv) proxySettings.push(`HTTP_PROXY: ${httpProxyEnv}`) |
| 29 | + if (allProxy) proxySettings.push(`ALL_PROXY: ${allProxy}`) |
| 30 | + |
| 31 | + // Check if running with proxy PAC file |
| 32 | + const args = process.argv.join(" ") |
| 33 | + const hasPacFile = args.includes("--proxy-pac-url") |
| 34 | + if (hasPacFile) { |
| 35 | + const pacMatch = args.match(/--proxy-pac-url[= ]([^ ]+)/) |
| 36 | + if (pacMatch) { |
| 37 | + proxySettings.push(`PAC file: ${pacMatch[1]}`) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + const hasProxyConfig = proxySettings.length > 0 |
| 42 | + const hasIssue = hasProxyConfig && !electronFetchEnabled |
| 43 | + |
| 44 | + return { |
| 45 | + hasProxyConfig, |
| 46 | + electronFetchEnabled, |
| 47 | + hasIssue, |
| 48 | + proxySettings, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Formats a helpful error message when proxy configuration issues are detected |
| 54 | + */ |
| 55 | +export function formatProxyErrorMessage( |
| 56 | + error: any, |
| 57 | + proxyInfo?: ReturnType<typeof detectProxyConfigurationIssue>, |
| 58 | +): string { |
| 59 | + const baseError = error?.message || "Connection error" |
| 60 | + |
| 61 | + if (!proxyInfo) { |
| 62 | + proxyInfo = detectProxyConfigurationIssue() |
| 63 | + } |
| 64 | + |
| 65 | + if (proxyInfo.hasIssue) { |
| 66 | + return `${baseError} |
| 67 | +
|
| 68 | +⚠️ **Proxy Configuration Issue Detected** |
| 69 | +
|
| 70 | +You have proxy settings configured but \`http.electronFetch\` is disabled (default). This can cause connection errors with API providers. |
| 71 | +
|
| 72 | +**Detected proxy settings:** |
| 73 | +${proxyInfo.proxySettings.map((s) => `• ${s}`).join("\n")} |
| 74 | +
|
| 75 | +**Solution:** |
| 76 | +1. Open VSCode Settings (Cmd/Ctrl + ,) |
| 77 | +2. Search for "http.electronFetch" |
| 78 | +3. Enable the setting (check the box) |
| 79 | +4. Restart VSCode and try again |
| 80 | +
|
| 81 | +**Alternative solutions:** |
| 82 | +• Use a different API provider that works with your proxy setup |
| 83 | +• Configure the extension to use axios-based providers when available |
| 84 | +• Temporarily disable proxy settings if not needed |
| 85 | +
|
| 86 | +For more information, see: https://github.com/microsoft/vscode/issues/12588` |
| 87 | + } |
| 88 | + |
| 89 | + // Check for other common connection errors |
| 90 | + if (baseError.includes("ECONNREFUSED")) { |
| 91 | + return `${baseError} |
| 92 | +
|
| 93 | +The connection was refused. Please check: |
| 94 | +• Is the API endpoint URL correct? |
| 95 | +• Is the service running and accessible? |
| 96 | +• Are there any firewall or network restrictions?` |
| 97 | + } |
| 98 | + |
| 99 | + if (baseError.includes("ETIMEDOUT") || baseError.includes("ESOCKETTIMEDOUT")) { |
| 100 | + return `${baseError} |
| 101 | +
|
| 102 | +The connection timed out. Please check: |
| 103 | +• Is the API endpoint accessible from your network? |
| 104 | +• Are you behind a corporate firewall or VPN? |
| 105 | +• Is the service experiencing high load?` |
| 106 | + } |
| 107 | + |
| 108 | + if (baseError.includes("ENOTFOUND")) { |
| 109 | + return `${baseError} |
| 110 | +
|
| 111 | +The hostname could not be resolved. Please check: |
| 112 | +• Is the API endpoint URL spelled correctly? |
| 113 | +• Do you have internet connectivity? |
| 114 | +• Are DNS settings configured correctly?` |
| 115 | + } |
| 116 | + |
| 117 | + return baseError |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Shows a warning notification if proxy configuration issues are detected |
| 122 | + */ |
| 123 | +export async function showProxyConfigurationWarning(): Promise<void> { |
| 124 | + const proxyInfo = detectProxyConfigurationIssue() |
| 125 | + |
| 126 | + if (proxyInfo.hasIssue) { |
| 127 | + const message = "Proxy detected but http.electronFetch is disabled. This may cause connection errors." |
| 128 | + const action = await vscode.window.showWarningMessage(message, "Enable electronFetch", "Learn More", "Dismiss") |
| 129 | + |
| 130 | + if (action === "Enable electronFetch") { |
| 131 | + // Open settings and navigate to the http.electronFetch setting |
| 132 | + await vscode.commands.executeCommand("workbench.action.openSettings", "http.electronFetch") |
| 133 | + } else if (action === "Learn More") { |
| 134 | + // Open the GitHub issue for more information |
| 135 | + await vscode.env.openExternal(vscode.Uri.parse("https://github.com/microsoft/vscode/issues/12588")) |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments