Skip to content

Commit 157669d

Browse files
committed
Adding error handling
1 parent 4df8021 commit 157669d

File tree

2 files changed

+51
-39
lines changed

2 files changed

+51
-39
lines changed

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

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -761,48 +761,60 @@ export class ChatController {
761761
const session = this.sessionStorage.getSession(message.tabID)
762762
// Check if user clicked on filePath in the contextList or in the fileListTree and perform the functionality accordingly.
763763
if (session.showDiffOnFileWrite) {
764-
// Create a temporary file path to show the diff view
765-
const pathToArchiveDir = path.join(tempDirPath, 'q-chat')
766-
const archivePathExists = await fs.existsDir(pathToArchiveDir)
767-
if (archivePathExists) {
768-
await fs.delete(pathToArchiveDir, { recursive: true })
769-
}
770-
await fs.mkdir(pathToArchiveDir)
771-
const resultArtifactsDir = path.join(pathToArchiveDir, 'resultArtifacts')
772-
await fs.mkdir(resultArtifactsDir)
773-
const tempFilePath = path.join(
774-
resultArtifactsDir,
775-
`temp-${path.basename((session.toolUse?.input as unknown as FsWriteParams).path)}`
776-
)
764+
try {
765+
// Create a temporary file path to show the diff view
766+
const pathToArchiveDir = path.join(tempDirPath, 'q-chat')
767+
const archivePathExists = await fs.existsDir(pathToArchiveDir)
768+
if (archivePathExists) {
769+
await fs.delete(pathToArchiveDir, { recursive: true })
770+
}
771+
await fs.mkdir(pathToArchiveDir)
772+
const resultArtifactsDir = path.join(pathToArchiveDir, 'resultArtifacts')
773+
await fs.mkdir(resultArtifactsDir)
774+
const tempFilePath = path.join(
775+
resultArtifactsDir,
776+
`temp-${path.basename((session.toolUse?.input as unknown as FsWriteParams).path)}`
777+
)
777778

778-
// If we have existing filePath copy file content from existing file to temporary file.
779-
const filePath = (session.toolUse?.input as any).path ?? message.filePath
780-
const fileExists = await fs.existsFile(filePath)
781-
if (fileExists) {
782-
const fileContent = await fs.readFileText(filePath)
783-
await fs.writeFile(tempFilePath, fileContent)
784-
}
779+
// If we have existing filePath copy file content from existing file to temporary file.
780+
const filePath = (session.toolUse?.input as any).path ?? message.filePath
781+
const fileExists = await fs.existsFile(filePath)
782+
if (fileExists) {
783+
const fileContent = await fs.readFileText(filePath)
784+
await fs.writeFile(tempFilePath, fileContent)
785+
}
785786

786-
// Create a deep clone of the toolUse object and pass this toolUse to FsWrite tool execution to get the modified temporary file.
787-
const clonedToolUse = structuredClone(session.toolUse)
788-
if (!clonedToolUse) {
789-
return
790-
}
791-
const input = clonedToolUse.input as unknown as FsWriteParams
792-
input.path = tempFilePath
787+
// Create a deep clone of the toolUse object and pass this toolUse to FsWrite tool execution to get the modified temporary file.
788+
const clonedToolUse = structuredClone(session.toolUse)
789+
if (!clonedToolUse) {
790+
return
791+
}
792+
const input = clonedToolUse.input as unknown as FsWriteParams
793+
input.path = tempFilePath
793794

794-
const fsWrite = new FsWrite(input)
795-
await fsWrite.invoke()
795+
const fsWrite = new FsWrite(input)
796+
await fsWrite.invoke()
796797

797-
// Check if fileExists=false, If yes, return instead of showing broken diff experience.
798-
if (!tempFilePath) {
799-
void vscode.window.showInformationMessage('Generated code changes have been reviewed and processed.')
800-
return
798+
// Check if fileExists=false, If yes, return instead of showing broken diff experience.
799+
if (!tempFilePath) {
800+
void vscode.window.showInformationMessage(
801+
'Generated code changes have been reviewed and processed.'
802+
)
803+
return
804+
}
805+
const leftUri = fileExists ? vscode.Uri.file(filePath) : vscode.Uri.from({ scheme: 'untitled' })
806+
const rightUri = vscode.Uri.file(tempFilePath ?? filePath)
807+
const fileName = path.basename(filePath)
808+
await vscode.commands.executeCommand(
809+
'vscode.diff',
810+
leftUri,
811+
rightUri,
812+
`${fileName} ${amazonQTabSuffix}`
813+
)
814+
} catch (error) {
815+
getLogger().error(`Unexpected error in diff view generation: ${error}`)
816+
void vscode.window.showErrorMessage(`Failed to open diff view.`)
801817
}
802-
const leftUri = fileExists ? vscode.Uri.file(filePath) : vscode.Uri.from({ scheme: 'untitled' })
803-
const rightUri = vscode.Uri.file(tempFilePath ?? filePath)
804-
const fileName = path.basename(filePath)
805-
await vscode.commands.executeCommand('vscode.diff', leftUri, rightUri, `${fileName} ${amazonQTabSuffix}`)
806818
} else {
807819
const lineRanges = session.contexts.get(message.filePath)
808820

packages/core/src/codewhispererChat/tools/toolUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ export class ToolUtils {
5353
static async invoke(tool: Tool, updates?: Writable): Promise<InvokeOutput> {
5454
switch (tool.type) {
5555
case ToolType.FsRead:
56-
return tool.tool.invoke(updates ?? undefined)
56+
return tool.tool.invoke(updates)
5757
case ToolType.FsWrite:
58-
return tool.tool.invoke(updates ?? undefined)
58+
return tool.tool.invoke(updates)
5959
case ToolType.ExecuteBash:
6060
return tool.tool.invoke(updates ?? undefined)
6161
case ToolType.ListDirectory:

0 commit comments

Comments
 (0)