Skip to content

Commit fc32bfd

Browse files
committed
improve JSON parse error handling
1 parent fffcdd3 commit fc32bfd

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

packages/core/src/codewhispererChat/controllers/chat/controller.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,12 +714,17 @@ export class ChatController {
714714
const toolUseError = toolUseWithError.error
715715
const toolResults: ToolResult[] = []
716716

717+
let response = ''
717718
if (toolUseError) {
718719
toolResults.push({
719720
content: [{ text: toolUseError.message }],
720721
toolUseId: toolUse.toolUseId,
721722
status: ToolResultStatus.ERROR,
722723
})
724+
if (toolUseError instanceof SyntaxError) {
725+
response =
726+
"Your toolUse input isn't valid. Please check the syntax and make sure the input is complete. If the input is large, break it down into multiple tool uses with smaller input."
727+
}
723728
} else {
724729
const result = ToolUtils.tryFromToolUse(toolUse)
725730
if ('type' in result) {
@@ -772,7 +777,7 @@ export class ChatController {
772777

773778
await this.generateResponse(
774779
{
775-
message: '',
780+
message: response,
776781
trigger: ChatTriggerType.ChatMessage,
777782
query: undefined,
778783
codeSelection: context?.focusAreaContext?.selectionInsideExtendedCodeBlock,

packages/core/src/codewhispererChat/controllers/chat/messenger/messenger.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,12 @@ export class Messenger {
273273
try {
274274
toolUse.input = JSON.parse(toolUseInput)
275275
} catch (error: any) {
276-
getLogger().error(`JSON parse error for toolUseInput: ${toolUseInput}`)
276+
getLogger().error(
277+
`JSON parse error: ${error.message} for toolUseInput: ${toolUseInput}`
278+
)
277279
// set toolUse.input to be empty valid json object
278280
toolUse.input = {}
279-
error.message = `Tool input has invalid JSON format: ${error.message}`
281+
error.message = `tooluse input is invalid: ${toolUseInput}`
280282
// throw it out to allow the error to be handled in the catch block
281283
throw error
282284
}

0 commit comments

Comments
 (0)