Skip to content

Commit 3dad415

Browse files
authored
Merge pull request #8301 from continuedev/pe/fetch
feat: return fetch errors to agent
2 parents 7bc531a + c6780c0 commit 3dad415

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

core/context/providers/URLContextProvider.ts

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,40 @@ export async function getUrlContextItems(
3333
query: string,
3434
fetchFn: FetchFunction,
3535
): Promise<ContextItem[]> {
36-
try {
37-
const url = new URL(query);
38-
const icon = await fetchFavicon(url);
39-
const resp = await fetchFn(url);
40-
const html = await resp.text();
36+
const url = new URL(query);
37+
const icon = await fetchFavicon(url);
38+
const resp = await fetchFn(url);
4139

42-
const dom = new JSDOM(html);
43-
let reader = new Readability(dom.window.document);
44-
let article = reader.parse();
45-
const content = article?.content || "";
46-
const markdown = NodeHtmlMarkdown.translate(
47-
content,
48-
{},
49-
undefined,
50-
undefined,
51-
);
40+
// Check if the response is not OK
41+
if (!resp.ok) {
42+
throw new Error(`HTTP ${resp.status} ${resp.statusText}`);
43+
}
44+
45+
const html = await resp.text();
5246

53-
const title = article?.title || url.pathname;
47+
const dom = new JSDOM(html);
48+
let reader = new Readability(dom.window.document);
49+
let article = reader.parse();
50+
const content = article?.content || "";
51+
const markdown = NodeHtmlMarkdown.translate(
52+
content,
53+
{},
54+
undefined,
55+
undefined,
56+
);
5457

55-
return [
56-
{
57-
icon,
58-
description: url.toString(),
59-
content: markdown,
60-
name: title,
61-
uri: {
62-
type: "url",
63-
value: url.toString(),
64-
},
58+
const title = article?.title || url.pathname;
59+
60+
return [
61+
{
62+
icon,
63+
description: url.toString(),
64+
content: markdown,
65+
name: title,
66+
uri: {
67+
type: "url",
68+
value: url.toString(),
6569
},
66-
];
67-
} catch (e) {
68-
console.log(e);
69-
return [];
70-
}
70+
},
71+
];
7172
}

core/tools/implementations/fetchUrlContent.vitest.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,13 @@ test("fetchUrlContent should add truncation warning with multiple truncated item
8989
expect(result[1].content.length).toBe(DEFAULT_FETCH_URL_CHAR_LIMIT);
9090
expect(result[2].name).toBe("Truncation warning");
9191
});
92+
93+
test("fetchUrlContent should propagate errors when URL fetch fails", async () => {
94+
const errorMessage = "HTTP 404 Not Found";
95+
96+
(getUrlContextItems as any).mockRejectedValue(new Error(errorMessage));
97+
98+
await expect(
99+
fetchUrlContentImpl({ url: "https://example.com/404" }, mockExtras),
100+
).rejects.toThrow(errorMessage);
101+
});

0 commit comments

Comments
 (0)