|
2 | 2 | import * as vscode from 'vscode'; |
3 | 3 |
|
4 | 4 | import { NEURO } from '@/constants'; |
5 | | -import { formatContext, getFence, getPositionContext, getVirtualCursor, getWorkspacePath, getWorkspaceUri, isBinary, isPathNeuroSafe, logOutput, normalizePath } from '@/utils'; |
| 5 | +import { formatContext, getFence, getPositionContext, getVirtualCursor, getWorkspacePath, getWorkspaceUri, isBinary, isPathNeuroSafe, logOutput, normalizePath, notifyOnCaughtException } from '@/utils'; |
6 | 6 | import { ActionData, contextNoAccess, RCEAction, actionValidationFailure, actionValidationAccept, ActionValidationResult, stripToActions } from '@/neuro_client_helper'; |
7 | 7 | import { CONFIG, PERMISSIONS, PermissionLevel, getPermissionLevel, isActionEnabled } from '@/config'; |
8 | 8 | import { targetedFileCreatedEvent, targetedFileDeletedEvent } from '@events/files'; |
@@ -43,8 +43,9 @@ async function getUriExistence(uri: vscode.Uri): Promise<boolean> { |
43 | 43 | try { |
44 | 44 | await vscode.workspace.fs.stat(uri); |
45 | 45 | return true; |
46 | | - } catch { |
47 | | - return false; |
| 46 | + } catch (erm: unknown) { |
| 47 | + if (erm instanceof vscode.FileSystemError && erm.code === 'FileNotFound') return false; |
| 48 | + else throw erm; |
48 | 49 | } |
49 | 50 | } |
50 | 51 |
|
@@ -250,7 +251,7 @@ export const fileActions = { |
250 | 251 | (actionData: ActionData) => targetedFileDeletedEvent(actionData.params?.path), |
251 | 252 | ], |
252 | 253 | validators: [neuroSafeDeleteValidation], |
253 | | - promptGenerator: (actionData: ActionData) => `delete "${actionData.params?.pathToDelete}".`, |
| 254 | + promptGenerator: (actionData: ActionData) => `delete "${actionData.params?.path}".`, |
254 | 255 | }, |
255 | 256 | } satisfies Record<string, RCEAction>; |
256 | 257 |
|
@@ -304,13 +305,19 @@ export function handleCreateFile(actionData: ActionData): string | undefined { |
304 | 305 | // If no error is thrown, the file already exists |
305 | 306 | NEURO.client?.sendContext(`Could not create file: File ${relativePath} already exists`); |
306 | 307 | return; |
307 | | - } catch { /* File does not exist, continue */ } |
| 308 | + } catch (erm: unknown) { |
| 309 | + if ((erm as vscode.FileSystemError).code !== 'FileNotFound') { |
| 310 | + notifyOnCaughtException('create_file', erm); |
| 311 | + return; |
| 312 | + }; |
| 313 | + /* else, file does not exist, continue */ |
| 314 | + } |
308 | 315 |
|
309 | 316 | // Create the file |
310 | 317 | try { |
311 | 318 | await vscode.workspace.fs.writeFile(fileUri, new Uint8Array(0)); |
312 | 319 | } catch (erm: unknown) { |
313 | | - logOutput('ERROR', `Failed to create file ${relativePath}: ${erm}`); |
| 320 | + notifyOnCaughtException('create_file', erm); |
314 | 321 | NEURO.client?.sendContext(`Failed to create file ${relativePath}`); |
315 | 322 | return; |
316 | 323 | } |
@@ -353,13 +360,19 @@ export function handleCreateFolder(actionData: ActionData): string | undefined { |
353 | 360 | // If no error is thrown, the folder already exists |
354 | 361 | NEURO.client?.sendContext(`Could not create folder: Folder ${relativePath} already exists`); |
355 | 362 | return; |
356 | | - } catch { /* Folder does not exist, continue */ } |
| 363 | + } catch (erm: unknown) { |
| 364 | + if ((erm as vscode.FileSystemError).code !== 'FileNotFound') { |
| 365 | + notifyOnCaughtException('create_folder', erm); |
| 366 | + return; |
| 367 | + } |
| 368 | + /* else, folder does not exist, continue */ |
| 369 | + } |
357 | 370 |
|
358 | 371 | // Create the folder |
359 | 372 | try { |
360 | 373 | await vscode.workspace.fs.createDirectory(folderUri); |
361 | 374 | } catch (erm: unknown) { |
362 | | - logOutput('ERROR', `Failed to create folder ${relativePath}: ${erm}`); |
| 375 | + notifyOnCaughtException('create_folder', erm); |
363 | 376 | NEURO.client?.sendContext(`Failed to create folder ${relativePath}`); |
364 | 377 | return; |
365 | 378 | } |
@@ -393,13 +406,19 @@ export function handleRenameFileOrFolder(actionData: ActionData): string | undef |
393 | 406 | // If no error is thrown, the new path already exists |
394 | 407 | NEURO.client?.sendContext(`Could not rename: ${newRelativePath} already exists`); |
395 | 408 | return; |
396 | | - } catch { /* New path does not exist, continue */ } |
| 409 | + } catch (erm: unknown) { |
| 410 | + if ((erm as vscode.FileSystemError).code !== 'FileNotFound') { |
| 411 | + notifyOnCaughtException('rename_file_or_folder', erm); |
| 412 | + return; |
| 413 | + }; |
| 414 | + /* New path does not exist, continue */ |
| 415 | + } |
397 | 416 |
|
398 | 417 | // Rename the file/folder |
399 | 418 | try { |
400 | 419 | await vscode.workspace.fs.rename(oldUri, newUri); |
401 | 420 | } catch (erm: unknown) { |
402 | | - logOutput('ERROR', `Failed to rename ${oldRelativePath} to ${newRelativePath}: ${erm}`); |
| 421 | + notifyOnCaughtException('rename_file_or_folder', erm); |
403 | 422 | NEURO.client?.sendContext(`Failed to rename ${oldRelativePath} to ${newRelativePath}`); |
404 | 423 | return; |
405 | 424 | } |
@@ -429,9 +448,14 @@ export function handleDeleteFileOrFolder(actionData: ActionData): string | undef |
429 | 448 | // Check if the path exists |
430 | 449 | try { |
431 | 450 | stat = await vscode.workspace.fs.stat(uri); |
432 | | - } catch { |
433 | | - NEURO.client?.sendContext(`Could not delete: ${relativePath} does not exist`); |
434 | | - return; |
| 451 | + } catch (erm: unknown) { |
| 452 | + if (erm instanceof vscode.FileSystemError && erm.code === 'FileNotFound') { |
| 453 | + NEURO.client?.sendContext(`Could not delete: ${relativePath} does not exist`); |
| 454 | + return; |
| 455 | + } else { |
| 456 | + notifyOnCaughtException('delete_file_or_folder', erm); |
| 457 | + return; |
| 458 | + } |
435 | 459 | } |
436 | 460 |
|
437 | 461 | // Check for correct recursive parameter |
@@ -570,7 +594,7 @@ export function handleOpenFile(actionData: ActionData): string | undefined { |
570 | 594 | } |
571 | 595 | } |
572 | 596 | } catch (erm: unknown) { |
573 | | - logOutput('ERROR', `Failed to open file ${relativePath}: ${erm}`); |
| 597 | + notifyOnCaughtException('open_file', erm); |
574 | 598 | NEURO.client?.sendContext(`Failed to open file ${relativePath}`); |
575 | 599 | } |
576 | 600 | } |
@@ -598,7 +622,8 @@ export function handleReadFile(actionData: ActionData): string | undefined { |
598 | 622 | }, |
599 | 623 | ); |
600 | 624 | } catch (erm: unknown) { |
601 | | - logOutput('ERROR', `Error occured while trying to access file ${file}: ${erm}`); |
| 625 | + notifyOnCaughtException('read_file', erm); |
| 626 | + NEURO.client?.sendContext(`Unable to read file ${file}`); |
602 | 627 | } |
603 | 628 | } |
604 | 629 |
|
|
0 commit comments