Improve applyAction function and add error handling

- Add compressText function to truncate long text.
- Wrap applyAction in try-catch block for error handling.
- Send error message to devchat if codeBlock type is 'command'.
This commit is contained in:
bobo.yang 2023-07-24 00:11:56 +08:00
parent d99548b3a0
commit 78d1db75f5

View File

@ -1,11 +1,34 @@
import * as vscode from 'vscode';
import { regInMessage, regOutMessage } from '../util/reg_messages';
import ActionManager from '../action/actionManager';
import { MessageHandler } from './messageHandler';
import { sendMessage } from './sendMessage';
import { logger } from '../util/logger';
function compressText(text: string, maxLength: number): string {
if (text.length <= maxLength) {
return text;
}
const halfLength = Math.floor(maxLength / 2);
return text.slice(0, halfLength) + " ... " + text.slice(-halfLength);
}
regInMessage({command: 'applyAction', actionName: '', codeBlock: { type: '', content: '', fileName: ''}});
export async function applyAction(message: any, panel: vscode.WebviewPanel|vscode.WebviewView): Promise<void> {
ActionManager.getInstance().applyAction(message.actionName, message.codeBlock);
try {
const result = await ActionManager.getInstance().applyAction(message.actionName, message.codeBlock);
if (message.codeBlock.type === 'command') {
// send error message to devchat
const newMessage = `run command: ${compressText(message.codeBlock.content, 100)}, result: {"exit_code": ${result.exitCode}, stdout: ${compressText(result.stdout, 100)}, stderr: ${compressText(result.stderr, 100)}}. Next step`;
MessageHandler.sendMessage(panel, { "command": "sendMessageSystem" });
sendMessage({command: 'sendMessage', text: newMessage}, panel);
}
} catch (error) {
logger.channel()?.error('Failed to parse code file content: ' + error);
logger.channel()?.show();
}
}