Skip to content

Commit 6796e1a

Browse files
committed
fix: address review feedback for nested try-catch pattern and add test coverage
- Remove nested try-catch pattern in src/core/mentions/index.ts for consistency - Add test coverage for RangeError handling in readFileTool - Ensure graceful error handling when isBinaryFile throws RangeError
1 parent ecb09af commit 6796e1a

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/core/mentions/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,18 @@ async function getFileOrFolderContent(
267267
const absoluteFilePath = path.resolve(absPath, entry.name)
268268
fileContentPromises.push(
269269
(async () => {
270+
let isBinary = false
271+
try {
272+
isBinary = await isBinaryFile(absoluteFilePath)
273+
} catch (error) {
274+
// If isBinaryFile throws an error (e.g., RangeError), treat as binary
275+
console.warn(`Error checking if file is binary for ${absoluteFilePath}:`, error)
276+
isBinary = true
277+
}
278+
if (isBinary) {
279+
return undefined
280+
}
270281
try {
271-
let isBinary = false
272-
try {
273-
isBinary = await isBinaryFile(absoluteFilePath)
274-
} catch (error) {
275-
// If isBinaryFile throws an error (e.g., RangeError), treat as binary
276-
console.warn(`Error checking if file is binary for ${absoluteFilePath}:`, error)
277-
isBinary = true
278-
}
279-
if (isBinary) {
280-
return undefined
281-
}
282282
const content = await extractTextFromFile(absoluteFilePath, maxReadFileLine)
283283
return `<file_content path="${filePath.toPosix()}">\n${content}\n</file_content>`
284284
} catch (error) {

src/core/tools/__tests__/readFileTool.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,5 +518,26 @@ describe("read_file tool XML output structure", () => {
518518
`<files>\n<file><path>${testFilePath}</path><error>Access to ${testFilePath} is blocked by the .rooignore file settings. You must try to continue in the task without using this file, or ask the user to update the .rooignore file.</error></file>\n</files>`,
519519
)
520520
})
521+
522+
it("should handle RangeError from isBinaryFile gracefully", async () => {
523+
// Setup - mock isBinaryFile to throw RangeError
524+
mockedIsBinaryFile.mockRejectedValue(new RangeError("Invalid array length"))
525+
mockedCountFileLines.mockResolvedValue(5)
526+
527+
// Execute - the main goal is to verify the error doesn't crash the application
528+
const result = await executeReadFileTool(
529+
{},
530+
{
531+
totalLines: 5,
532+
},
533+
)
534+
535+
// Verify that the file is processed (the error is handled gracefully)
536+
expect(result).toContain(`<file><path>${testFilePath}</path>`)
537+
538+
// Verify that we get a valid XML response (not an error)
539+
expect(result).toMatch(/<files>.*<\/files>/s)
540+
expect(result).not.toContain("<error>")
541+
})
521542
})
522543
})

0 commit comments

Comments
 (0)