Skip to content

Commit 61bfb60

Browse files
committed
add try/catch on JSON parsing in case of invalid content
1 parent 8ba9eec commit 61bfb60

File tree

1 file changed

+58
-19
lines changed

1 file changed

+58
-19
lines changed

webview-ui/src/components/chat/ChatView.tsx

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
4545
if (!message.text) {
4646
return true
4747
}
48-
const tool = JSON.parse(message.text)
49-
return ["readFile", "listFiles", "listFilesTopLevel", "listFilesRecursive", "listCodeDefinitionNames", "searchFiles"].includes(tool.tool)
48+
try {
49+
const tool = JSON.parse(message.text)
50+
return ["readFile", "listFiles", "listFilesTopLevel", "listFilesRecursive", "listCodeDefinitionNames", "searchFiles"].includes(tool.tool)
51+
} catch (error) {
52+
console.error("Error parsing tool JSON:", error)
53+
return false
54+
}
5055
}
5156
return false
5257
}, [])
@@ -56,8 +61,13 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
5661
if (!message.text) {
5762
return true
5863
}
59-
const tool = JSON.parse(message.text)
60-
return ["editedExistingFile", "appliedDiff", "newFileCreated"].includes(tool.tool)
64+
try {
65+
const tool = JSON.parse(message.text)
66+
return ["editedExistingFile", "appliedDiff", "newFileCreated"].includes(tool.tool)
67+
} catch (error) {
68+
console.error("Error parsing tool JSON:", error)
69+
return false
70+
}
6171
}
6272
return false
6373
}, [])
@@ -67,12 +77,18 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
6777
if (!message.text) {
6878
return true
6979
}
70-
const mcpServerUse = JSON.parse(message.text) as { type: string; serverName: string; toolName: string }
71-
if (mcpServerUse.type === "use_mcp_tool") {
72-
const server = mcpServers?.find((s: McpServer) => s.name === mcpServerUse.serverName)
73-
const tool = server?.tools?.find((t: McpTool) => t.name === mcpServerUse.toolName)
74-
return tool?.alwaysAllow || false
80+
try {
81+
const mcpServerUse = JSON.parse(message.text) as { type: string; serverName: string; toolName: string }
82+
if (mcpServerUse.type === "use_mcp_tool") {
83+
const server = mcpServers?.find((s: McpServer) => s.name === mcpServerUse.serverName)
84+
const tool = server?.tools?.find((t: McpTool) => t.name === mcpServerUse.toolName)
85+
return tool?.alwaysAllow || false
86+
}
87+
} catch (error) {
88+
console.error("Error parsing MCP server use JSON:", error)
89+
return false
7590
}
91+
return false
7692
}
7793
return false
7894
}, [mcpServers])
@@ -182,7 +198,13 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
182198
setTextAreaDisabled(isPartial)
183199
setClineAsk("tool")
184200
setEnableButtons(!isPartial)
185-
const tool = JSON.parse(lastMessage.text || "{}") as ClineSayTool
201+
let tool: ClineSayTool;
202+
try {
203+
tool = JSON.parse(lastMessage.text || "{}") as ClineSayTool
204+
} catch (error) {
205+
console.error("Error parsing tool JSON:", error)
206+
tool = {} as ClineSayTool
207+
}
186208
switch (tool.tool) {
187209
case "editedExistingFile":
188210
case "appliedDiff":
@@ -323,10 +345,15 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
323345
} else {
324346
const lastApiReqStarted = findLast(modifiedMessages, (message) => message.say === "api_req_started")
325347
if (lastApiReqStarted && lastApiReqStarted.text != null && lastApiReqStarted.say === "api_req_started") {
326-
const cost = JSON.parse(lastApiReqStarted.text).cost
327-
if (cost === undefined) {
328-
// api request has not finished yet
329-
return true
348+
try {
349+
const cost = JSON.parse(lastApiReqStarted.text).cost
350+
if (cost === undefined) {
351+
// api request has not finished yet
352+
return true
353+
}
354+
} catch (error) {
355+
console.error("Error parsing API request cost JSON:", error)
356+
return true // Assume streaming is still in progress if we can't parse the cost
330357
}
331358
}
332359
}
@@ -621,9 +648,16 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
621648
// get last api_req_started in currentGroup to check if it's cancelled. If it is then this api req is not part of the current browser session
622649
const lastApiReqStarted = [...currentGroup].reverse().find((m) => m.say === "api_req_started")
623650
if (lastApiReqStarted?.text != null) {
624-
const info = JSON.parse(lastApiReqStarted.text)
625-
const isCancelled = info.cancelReason != null
626-
if (isCancelled) {
651+
try {
652+
const info = JSON.parse(lastApiReqStarted.text)
653+
const isCancelled = info.cancelReason != null
654+
if (isCancelled) {
655+
endBrowserSession()
656+
result.push(message)
657+
return
658+
}
659+
} catch (error) {
660+
console.error("Error parsing API request JSON:", error)
627661
endBrowserSession()
628662
result.push(message)
629663
return
@@ -636,8 +670,13 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
636670

637671
// Check if this is a close action
638672
if (message.say === "browser_action") {
639-
const browserAction = JSON.parse(message.text || "{}") as ClineSayBrowserAction
640-
if (browserAction.action === "close") {
673+
try {
674+
const browserAction = JSON.parse(message.text || "{}") as ClineSayBrowserAction
675+
if (browserAction.action === "close") {
676+
endBrowserSession()
677+
}
678+
} catch (error) {
679+
console.error("Error parsing browser action JSON:", error)
641680
endBrowserSession()
642681
}
643682
}

0 commit comments

Comments
 (0)