Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/ai/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,20 @@ export async function postChatCompletion(
signal: controller.signal,
});
} catch (e) {
throw new ProviderError(e instanceof Error ? e.message : 'Network error');
if (e instanceof Error) {
if (e.name === 'AbortError') {
throw new ProviderError(`Request timed out after 30s connecting to ${url}`);
}
const cause = (e as any).cause;
if (cause?.code === 'ECONNREFUSED') {
throw new ProviderError(`Connection refused to ${url}. Is the server running?`);
}
if (cause?.code === 'ENOTFOUND') {
throw new ProviderError(`Address not found: ${url}. Check your settings.`);
}
throw new ProviderError(`Network error: ${e.message}`);
}
throw new ProviderError('Network error');
}

if (!res.ok) {
Expand Down
29 changes: 29 additions & 0 deletions src/test/http_error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as assert from 'assert';
import { postChatCompletion } from '../ai/http';
import { ProviderError } from '../ai/errors';

suite('HTTP Error Handling', () => {
test('ECONNREFUSED returns friendly message', async () => {
const url = 'http://localhost:11111/v1/chat/completions'; // Port likely closed
try {
await postChatCompletion(url, undefined, { model: 'test', messages: [] });
assert.fail('Should have thrown');
} catch (e) {
assert.ok(e instanceof ProviderError, `Expected ProviderError but got ${e}`);
assert.ok(e.message.includes('Connection refused'), `Expected 'Connection refused' in '${e.message}'`);
assert.ok(e.message.includes(url), `Expected URL in error message '${e.message}'`);
}
});

test('ENOTFOUND returns friendly message', async () => {
const url = 'http://nonexistent-domain-xyz.local/v1/chat/completions';
try {
await postChatCompletion(url, undefined, { model: 'test', messages: [] });
assert.fail('Should have thrown');
} catch (e) {
assert.ok(e instanceof ProviderError, `Expected ProviderError but got ${e}`);
assert.ok(e.message.includes('Address not found'), `Expected 'Address not found' in '${e.message}'`);
assert.ok(e.message.includes(url), `Expected URL in error message '${e.message}'`);
}
});
});
Loading