Skip to content

Commit a1d33b5

Browse files
committed
fix: improve error handling in parseMentions for better user feedback and AI integration
1 parent 20e0d93 commit a1d33b5

File tree

2 files changed

+24
-44
lines changed

2 files changed

+24
-44
lines changed

src/core/mentions/__tests__/index.spec.ts

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,7 @@ vi.mock("vscode", () => ({
1515

1616
// Mock i18n
1717
vi.mock("../../../i18n", () => ({
18-
t: vi.fn((key: string, params?: any) => {
19-
const translations: Record<string, string> = {
20-
"common:errors.url_timeout": "The website took too long to load (timeout).",
21-
"common:errors.url_not_found": "The website address could not be found.",
22-
"common:errors.no_internet": "No internet connection.",
23-
"common:errors.url_forbidden": "Access to this website is forbidden.",
24-
"common:errors.url_page_not_found": "The page was not found.",
25-
"common:errors.url_fetch_failed": `Failed to fetch URL content: ${params?.error || "Unknown error"}`,
26-
"common:errors.url_fetch_error_with_url": `Error fetching content for ${params?.url}: ${params?.error}`,
27-
}
28-
return translations[key] || key
29-
}),
18+
t: vi.fn((key: string) => key),
3019
}))
3120

3221
describe("parseMentions - URL error handling", () => {
@@ -51,10 +40,8 @@ describe("parseMentions - URL error handling", () => {
5140
const result = await parseMentions("Check @https://example.com", "/test", mockUrlContentFetcher)
5241

5342
expect(consoleErrorSpy).toHaveBeenCalledWith("Error fetching URL https://example.com:", timeoutError)
54-
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith(
55-
"Error fetching content for https://example.com: The website took too long to load (timeout).",
56-
)
57-
expect(result).toContain("Error fetching content: The website took too long to load (timeout).")
43+
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith("common:errors.url_fetch_error_with_url")
44+
expect(result).toContain("Error fetching content: Navigation timeout of 30000 ms exceeded")
5845
})
5946

6047
it("should handle DNS resolution errors", async () => {
@@ -63,10 +50,8 @@ describe("parseMentions - URL error handling", () => {
6350

6451
const result = await parseMentions("Check @https://nonexistent.example", "/test", mockUrlContentFetcher)
6552

66-
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith(
67-
"Error fetching content for https://nonexistent.example: The website address could not be found.",
68-
)
69-
expect(result).toContain("Error fetching content: The website address could not be found.")
53+
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith("common:errors.url_fetch_error_with_url")
54+
expect(result).toContain("Error fetching content: net::ERR_NAME_NOT_RESOLVED")
7055
})
7156

7257
it("should handle network disconnection errors", async () => {
@@ -75,10 +60,8 @@ describe("parseMentions - URL error handling", () => {
7560

7661
const result = await parseMentions("Check @https://example.com", "/test", mockUrlContentFetcher)
7762

78-
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith(
79-
"Error fetching content for https://example.com: No internet connection.",
80-
)
81-
expect(result).toContain("Error fetching content: No internet connection.")
63+
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith("common:errors.url_fetch_error_with_url")
64+
expect(result).toContain("Error fetching content: net::ERR_INTERNET_DISCONNECTED")
8265
})
8366

8467
it("should handle 403 Forbidden errors", async () => {
@@ -87,10 +70,8 @@ describe("parseMentions - URL error handling", () => {
8770

8871
const result = await parseMentions("Check @https://example.com", "/test", mockUrlContentFetcher)
8972

90-
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith(
91-
"Error fetching content for https://example.com: Access to this website is forbidden.",
92-
)
93-
expect(result).toContain("Error fetching content: Access to this website is forbidden.")
73+
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith("common:errors.url_fetch_error_with_url")
74+
expect(result).toContain("Error fetching content: 403 Forbidden")
9475
})
9576

9677
it("should handle 404 Not Found errors", async () => {
@@ -99,10 +80,8 @@ describe("parseMentions - URL error handling", () => {
9980

10081
const result = await parseMentions("Check @https://example.com/missing", "/test", mockUrlContentFetcher)
10182

102-
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith(
103-
"Error fetching content for https://example.com/missing: The page was not found.",
104-
)
105-
expect(result).toContain("Error fetching content: The page was not found.")
83+
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith("common:errors.url_fetch_error_with_url")
84+
expect(result).toContain("Error fetching content: 404 Not Found")
10685
})
10786

10887
it("should handle generic errors with fallback message", async () => {
@@ -111,10 +90,8 @@ describe("parseMentions - URL error handling", () => {
11190

11291
const result = await parseMentions("Check @https://example.com", "/test", mockUrlContentFetcher)
11392

114-
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith(
115-
"Error fetching content for https://example.com: Failed to fetch URL content: Some unexpected error",
116-
)
117-
expect(result).toContain("Error fetching content: Failed to fetch URL content: Some unexpected error")
93+
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith("common:errors.url_fetch_error_with_url")
94+
expect(result).toContain("Error fetching content: Some unexpected error")
11895
})
11996

12097
it("should handle non-Error objects thrown", async () => {
@@ -123,9 +100,7 @@ describe("parseMentions - URL error handling", () => {
123100

124101
const result = await parseMentions("Check @https://example.com", "/test", mockUrlContentFetcher)
125102

126-
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith(
127-
expect.stringContaining("Error fetching content for https://example.com:"),
128-
)
103+
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith("common:errors.url_fetch_error_with_url")
129104
expect(result).toContain("Error fetching content:")
130105
})
131106

@@ -180,6 +155,6 @@ describe("parseMentions - URL error handling", () => {
180155
expect(result).toContain('<url_content url="https://example1.com">')
181156
expect(result).toContain("# First Site")
182157
expect(result).toContain('<url_content url="https://example2.com">')
183-
expect(result).toContain("Error fetching content: The website took too long to load (timeout).")
158+
expect(result).toContain("Error fetching content: timeout")
184159
})
185160
})

src/core/mentions/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,18 @@ export async function parseMentions(
129129
} catch (error) {
130130
console.error(`Error fetching URL ${mention}:`, error)
131131

132-
// Get user-friendly error message
133-
const errorMessage = getUrlErrorMessage(error)
132+
// Get raw error message for AI
133+
const rawErrorMessage = error instanceof Error ? error.message : String(error)
134+
135+
// Get localized error message for UI notification
136+
const localizedErrorMessage = getUrlErrorMessage(error)
134137

135138
vscode.window.showErrorMessage(
136-
t("common:errors.url_fetch_error_with_url", { url: mention, error: errorMessage }),
139+
t("common:errors.url_fetch_error_with_url", { url: mention, error: localizedErrorMessage }),
137140
)
138-
result = `Error fetching content: ${errorMessage}`
141+
142+
// Send raw error message to AI model
143+
result = `Error fetching content: ${rawErrorMessage}`
139144
}
140145
}
141146
parsedText += `\n\n<url_content url="${mention}">\n${result}\n</url_content>`

0 commit comments

Comments
 (0)