refactor(core): improve error handling for setGlobalProxy (#12437)

This commit is contained in:
Allen Hutchison
2025-11-03 10:13:52 -08:00
committed by GitHub
parent 19ea68b838
commit 9d642f3bb1
4 changed files with 85 additions and 16 deletions

View File

@@ -145,6 +145,20 @@ vi.mock('../agents/subagent-tool-wrapper.js', () => ({
SubagentToolWrapper: vi.fn(),
}));
const mockCoreEvents = vi.hoisted(() => ({
emitFeedback: vi.fn(),
}));
const mockSetGlobalProxy = vi.hoisted(() => vi.fn());
vi.mock('../utils/events.js', () => ({
coreEvents: mockCoreEvents,
}));
vi.mock('../utils/fetch.js', () => ({
setGlobalProxy: mockSetGlobalProxy,
}));
import { BaseLlmClient } from '../core/baseLlmClient.js';
import { tokenLimit } from '../core/tokenLimits.js';
import { uiTelemetryService } from '../telemetry/index.js';
@@ -912,6 +926,63 @@ describe('Server Config (config.ts)', () => {
expect(config.getTruncateToolOutputThreshold()).toBe(50000);
});
});
describe('Proxy Configuration Error Handling', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('should call setGlobalProxy when proxy is configured', () => {
const paramsWithProxy: ConfigParameters = {
...baseParams,
proxy: 'http://proxy.example.com:8080',
};
new Config(paramsWithProxy);
expect(mockSetGlobalProxy).toHaveBeenCalledWith(
'http://proxy.example.com:8080',
);
});
it('should not call setGlobalProxy when proxy is not configured', () => {
new Config(baseParams);
expect(mockSetGlobalProxy).not.toHaveBeenCalled();
});
it('should emit error feedback when setGlobalProxy throws an error', () => {
const proxyError = new Error('Invalid proxy URL');
mockSetGlobalProxy.mockImplementation(() => {
throw proxyError;
});
const paramsWithProxy: ConfigParameters = {
...baseParams,
proxy: 'invalid-proxy',
};
new Config(paramsWithProxy);
expect(mockCoreEvents.emitFeedback).toHaveBeenCalledWith(
'error',
'Invalid proxy configuration detected. Check debug drawer for more details (F12)',
proxyError,
);
});
it('should not emit error feedback when setGlobalProxy succeeds', () => {
mockSetGlobalProxy.mockImplementation(() => {
// Success - no error thrown
});
const paramsWithProxy: ConfigParameters = {
...baseParams,
proxy: 'http://proxy.example.com:8080',
};
new Config(paramsWithProxy);
expect(mockCoreEvents.emitFeedback).not.toHaveBeenCalled();
});
});
});
describe('setApprovalMode with folder trust', () => {

View File

@@ -76,6 +76,7 @@ import type { UserTierId } from '../code_assist/types.js';
import { AgentRegistry } from '../agents/registry.js';
import { setGlobalProxy } from '../utils/fetch.js';
import { SubagentToolWrapper } from '../agents/subagent-tool-wrapper.js';
import { coreEvents } from '../utils/events.js';
export enum ApprovalMode {
DEFAULT = 'default',
@@ -582,8 +583,17 @@ export class Config {
initializeTelemetry(this);
}
if (this.getProxy()) {
setGlobalProxy(this.getProxy() as string);
const proxy = this.getProxy();
if (proxy) {
try {
setGlobalProxy(proxy);
} catch (error) {
coreEvents.emitFeedback(
'error',
'Invalid proxy configuration detected. Check debug drawer for more details (F12)',
error,
);
}
}
this.geminiClient = new GeminiClient(this);
this.modelRouterService = new ModelRouterService(this);

View File

@@ -21,11 +21,7 @@ import { getErrorMessage } from '../utils/errors.js';
import type { Config } from '../config/config.js';
import { ApprovalMode, DEFAULT_GEMINI_FLASH_MODEL } from '../config/config.js';
import { getResponseText } from '../utils/partUtils.js';
import {
fetchWithTimeout,
isPrivateIp,
setGlobalProxy,
} from '../utils/fetch.js';
import { fetchWithTimeout, isPrivateIp } from '../utils/fetch.js';
import { convert } from 'html-to-text';
import {
logWebFetchFallbackAttempt,
@@ -415,10 +411,6 @@ export class WebFetchTool extends BaseDeclarativeTool<
false, // canUpdateOutput
messageBus,
);
const proxy = config.getProxy();
if (proxy) {
setGlobalProxy(proxy);
}
}
protected override validateToolParamValues(

View File

@@ -58,9 +58,5 @@ export async function fetchWithTimeout(
}
export function setGlobalProxy(proxy: string) {
try {
setGlobalDispatcher(new ProxyAgent(proxy));
} catch (e) {
console.error(`Failed to set proxy: ${getErrorMessage(e)}`);
}
setGlobalDispatcher(new ProxyAgent(proxy));
}