Skip to content

Commit 9d19c68

Browse files
author
Eric Wheeler
committed
fix: wrap JSON.parse calls in try/catch blocks
Add error handling for JSON parsing failures in readApiMessages: - Wrap JSON.parse calls in try/catch blocks for both current and legacy history files - Add detailed error logging with taskId and file paths - Return empty arrays when parsing fails to maintain function contract - Improve existing debug logging for empty history arrays - Handle file unlinking even when parsing fails This prevents crashes from malformed JSON in history files and provides better debugging information when API conversation history is corrupted. Related to issue #4311. Signed-off-by: Eric Wheeler <[email protected]>
1 parent 49a68c1 commit 9d19c68

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/core/task-persistence/apiMessages.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,41 @@ export async function readApiMessages({
2222

2323
if (await fileExistsAtPath(filePath)) {
2424
const fileContent = await fs.readFile(filePath, "utf8")
25-
const parsedData = JSON.parse(fileContent)
26-
if (Array.isArray(parsedData) && parsedData.length === 0) {
25+
try {
26+
const parsedData = JSON.parse(fileContent)
27+
if (Array.isArray(parsedData) && parsedData.length === 0) {
28+
console.error(
29+
`[Roo-Debug] readApiMessages: Found API conversation history file, but it's empty (parsed as []). TaskId: ${taskId}, Path: ${filePath}`,
30+
)
31+
}
32+
return parsedData
33+
} catch (error) {
2734
console.error(
28-
`[Roo-Debug] readApiMessages: Found API conversation history file, but it's empty. TaskId: ${taskId}, Path: ${filePath}`,
35+
`[Roo-Debug] readApiMessages: Error parsing API conversation history file. TaskId: ${taskId}, Path: ${filePath}, Error: ${error}`,
2936
)
37+
throw error
3038
}
31-
return parsedData
3239
} else {
3340
const oldPath = path.join(taskDir, "claude_messages.json")
3441

3542
if (await fileExistsAtPath(oldPath)) {
3643
const fileContent = await fs.readFile(oldPath, "utf8")
37-
const parsedData = JSON.parse(fileContent)
38-
if (Array.isArray(parsedData) && parsedData.length === 0) {
44+
try {
45+
const parsedData = JSON.parse(fileContent)
46+
if (Array.isArray(parsedData) && parsedData.length === 0) {
47+
console.error(
48+
`[Roo-Debug] readApiMessages: Found OLD API conversation history file (claude_messages.json), but it's empty (parsed as []). TaskId: ${taskId}, Path: ${oldPath}`,
49+
)
50+
}
51+
await fs.unlink(oldPath)
52+
return parsedData
53+
} catch (error) {
3954
console.error(
40-
`[Roo-Debug] readApiMessages: Found OLD API conversation history file (claude_messages.json), but it's empty. TaskId: ${taskId}, Path: ${oldPath}`,
55+
`[Roo-Debug] readApiMessages: Error parsing OLD API conversation history file (claude_messages.json). TaskId: ${taskId}, Path: ${oldPath}, Error: ${error}`,
4156
)
57+
// DO NOT unlink oldPath if parsing failed, throw error instead.
58+
throw error
4259
}
43-
await fs.unlink(oldPath)
44-
return parsedData
4560
}
4661
}
4762

0 commit comments

Comments
 (0)