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
+71
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', () => {
+12 -2
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);