From 4445c5ea506edc1c59535c0d3ae4b693c8c4d3c0 Mon Sep 17 00:00:00 2001 From: E44 <129310925+programmer-44@users.noreply.github.com> Date: Sun, 23 Nov 2025 00:35:34 +0100 Subject: [PATCH] chore(control): add shell command error handling --- control/frontend/src/ts/api_handler.ts | 35 +++++++++++++++----------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/control/frontend/src/ts/api_handler.ts b/control/frontend/src/ts/api_handler.ts index 0cd848d..03c6949 100644 --- a/control/frontend/src/ts/api_handler.ts +++ b/control/frontend/src/ts/api_handler.ts @@ -43,8 +43,7 @@ export async function get_file_data(ip: string, path: string): Promise { - const raw_response = await run_shell_command(ip, `cd ".${path}" && tree -Js`); + const command = `cd ".${path}" && tree -Js` + const raw_response = await run_shell_command(ip, command); if (!raw_response.ok || !raw_response.json) return null; const json_response = raw_response.json as ShellCommandResponse; - if (is_cd_directory_error(ip, json_response)) return null; + if (handle_shell_error(ip, json_response, command, true)) return null; return (JSON.parse(json_response.stdout.trim()) as [TreeElement, any])[0].contents || null; } @@ -105,16 +106,22 @@ export async function create_folders(ip: string, path: string, folder_names: str command += `&& mkdir "${part}" && cd "${part}/"`; } - await run_shell_command(ip, command); + const raw_response = await run_shell_command(ip, command); + if (!raw_response.ok || !raw_response.json) return; + const json_response = raw_response.json as ShellCommandResponse; + handle_shell_error(ip, json_response, command, true); } export async function delete_files(ip: string, current_path: string, file_names: string[]) { - let del_string: string = ''; + let command: string = 'cd ".${current_path}"'; for (const file_name of file_names) { - del_string += `&& rm -r "${file_name}"`; + command += ` && rm -r "${file_name}"`; } - await run_shell_command(ip, `cd ".${current_path}" ${del_string}`); - + await run_shell_command(ip, command); + const raw_response = await run_shell_command(ip, command); + if (!raw_response.ok || !raw_response.json) return; + const json_response = raw_response.json as ShellCommandResponse; + handle_shell_error(ip, json_response, command, true); } @@ -196,14 +203,14 @@ async function request(url: string, options: { method: string, headers?: Record< -function is_cd_directory_error(ip: string, shell_response: ShellCommandResponse): boolean { +function handle_shell_error(ip: string, shell_response: ShellCommandResponse, shell_command: string, command_includs_cd: boolean): boolean { if (shell_response.exitCode !== 0) { - if (shell_response.stderr && /bash: line \d+: cd: .+: No such file or directory/.test(shell_response.stderr)) { + if (command_includs_cd && shell_response.stderr && /bash: line \d+: cd: .+: No such file or directory/.test(shell_response.stderr)) { console.log("current file_path does not exist on display:", ip); return true; } console.error(shell_response); - notifications.push("error", "Fehler in ShellCommand", `Fehlercode: ${shell_response.exitCode}\nFehler: ${shell_response.stderr ?? ''}`) + notifications.push("error", `Fehler ${shell_response.exitCode} in API-Shell`, `${shell_command}\nFehler: ${shell_response.stderr}`); return true; } if (shell_response.stdout.trim() === '') return true;