Skip to content

Commit f37deb4

Browse files
committed
fix: replace JSON.parse with safeJsonParse for improved error handling
1 parent 638ca0e commit f37deb4

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import { QueuedMessages } from "./QueuedMessages"
5858
import DismissibleUpsell from "../common/DismissibleUpsell"
5959
import { useCloudUpsell } from "@src/hooks/useCloudUpsell"
6060
import { Cloud } from "lucide-react"
61+
import { safeJsonParse } from "../../../../src/shared/safeJsonParse"
6162

6263
export interface ChatViewProps {
6364
isHidden: boolean
@@ -547,16 +548,18 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
547548
lastApiReqStarted.text !== undefined &&
548549
lastApiReqStarted.say === "api_req_started"
549550
) {
550-
const info = JSON.parse(lastApiReqStarted.text)
551+
const info = safeJsonParse(lastApiReqStarted.text)
551552

552553
// If cancelReason is defined, the stream has been cancelled (terminal state)
553-
if (info.cancelReason !== undefined) {
554-
return false
555-
}
554+
if (typeof info === "object" && info !== null) {
555+
if ("cancelReason" in info && info.cancelReason !== undefined) {
556+
return false
557+
}
556558

557-
// Otherwise, check if cost is defined to determine if streaming is complete
558-
if (info.cost === undefined) {
559-
return true // API request has not finished yet.
559+
// Otherwise, check if cost is defined to determine if streaming is complete
560+
if ("cost" in info && info.cost !== undefined) {
561+
return true // API request has not finished yet.
562+
}
560563
}
561564
}
562565
}

0 commit comments

Comments
 (0)