Skip to content

Commit 79ba1d9

Browse files
KJ7LNWEric Wheeler
authored andcommitted
Add proper error handling for API conversation history issues (RooCodeInc#4312)
Co-authored-by: Eric Wheeler <[email protected]>
1 parent 139a449 commit 79ba1d9

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/core/task-persistence/apiMessages.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,49 @@ export async function readApiMessages({
2121
const filePath = path.join(taskDir, GlobalFileNames.apiConversationHistory)
2222

2323
if (await fileExistsAtPath(filePath)) {
24-
return JSON.parse(await fs.readFile(filePath, "utf8"))
24+
const fileContent = await fs.readFile(filePath, "utf8")
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) {
34+
console.error(
35+
`[Roo-Debug] readApiMessages: Error parsing API conversation history file. TaskId: ${taskId}, Path: ${filePath}, Error: ${error}`,
36+
)
37+
throw error
38+
}
2539
} else {
2640
const oldPath = path.join(taskDir, "claude_messages.json")
2741

2842
if (await fileExistsAtPath(oldPath)) {
29-
const data = JSON.parse(await fs.readFile(oldPath, "utf8"))
30-
await fs.unlink(oldPath)
31-
return data
43+
const fileContent = await fs.readFile(oldPath, "utf8")
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) {
54+
console.error(
55+
`[Roo-Debug] readApiMessages: Error parsing OLD API conversation history file (claude_messages.json). TaskId: ${taskId}, Path: ${oldPath}, Error: ${error}`,
56+
)
57+
// DO NOT unlink oldPath if parsing failed, throw error instead.
58+
throw error
59+
}
3260
}
3361
}
3462

63+
// If we reach here, neither the new nor the old history file was found.
64+
console.error(
65+
`[Roo-Debug] readApiMessages: API conversation history file not found for taskId: ${taskId}. Expected at: ${filePath}`,
66+
)
3567
return []
3668
}
3769

0 commit comments

Comments
 (0)