fix: handle setApprovalMode error in untrusted folders

Add try/catch to gracefully handle errors when setting privileged
modes (yolo/auto-edit) in untrusted folders, returning an error
message instead of throwing.
This commit is contained in:
Tu Shaokun 2026-01-06 22:04:43 +08:00
parent bfe7298858
commit 0878ee4cbd
2 changed files with 28 additions and 1 deletions

View file

@ -146,6 +146,25 @@ describe('approvalModeCommand', () => {
});
});
describe('untrusted folder handling', () => {
it('should return error when setApprovalMode throws (e.g., untrusted folder)', async () => {
const errorMessage =
'Cannot enable privileged approval modes in an untrusted folder.';
mockSetApprovalMode.mockImplementation(() => {
throw new Error(errorMessage);
});
const result = (await approvalModeCommand.action?.(
mockContext,
'yolo',
)) as MessageActionReturn;
expect(result.type).toBe('message');
expect(result.messageType).toBe('error');
expect(result.content).toBe(errorMessage);
});
});
it('should not have subcommands', () => {
expect(approvalModeCommand.subCommands).toBeUndefined();
});

View file

@ -63,7 +63,15 @@ export const approvalModeCommand: SlashCommand = {
// Set the mode for current session only (not persisted)
const { config } = context.services;
if (config) {
config.setApprovalMode(mode);
try {
config.setApprovalMode(mode);
} catch (e) {
return {
type: 'message',
messageType: 'error',
content: (e as Error).message,
};
}
}
return {