diff --git a/control/frontend/src/lib/ts/api_handler.ts b/control/frontend/src/lib/ts/api_handler.ts index f4a6205..72368e1 100755 --- a/control/frontend/src/lib/ts/api_handler.ts +++ b/control/frontend/src/lib/ts/api_handler.ts @@ -84,9 +84,10 @@ export async function get_file_data( `; const raw_response = await run_shell_command(ip, command); if (!raw_response.ok || !raw_response.json) return null; + if (!raw_response.ok) return null; + if (handle_shell_error(ip, raw_response, command, true)) return null; + const json_response = raw_response.json as ShellCommandResponse; - if (json_response.exitCode === 0 && json_response.stdout.trim() === '') return []; - if (handle_shell_error(ip, json_response, command, true)) return null; if (json_response.stdout.trim() === '') return null; const response: FileInfo[] = json_response.stdout @@ -116,11 +117,10 @@ export async function get_file_data( export async function get_file_tree_data(ip: string, path: string): Promise { const command = `cd ".${path}" && tree -Js`; const raw_response = await run_shell_command(ip, command); + if (!raw_response.ok) return null; + if (handle_shell_error(ip, raw_response, command, true)) return null; - if (!raw_response.ok || !raw_response.json) return null; const json_response = raw_response.json as ShellCommandResponse; - if (handle_shell_error(ip, json_response, command, true)) return null; - const tree_element: TreeElement | null = JSON.parse(json_response.stdout.trim())[0] || null; return tree_element?.contents || null; @@ -130,9 +130,8 @@ export async function create_path(ip: string, path: string): Promise { const command = `mkdir -p ".${path}"`; 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); + if (!raw_response.ok) return; + handle_shell_error(ip, raw_response, command, true); } export async function rename_file( @@ -144,9 +143,8 @@ export async function rename_file( const command: string = `cd ".${path}" && mv "${old_file_name}" "${new_file_name}"`; 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); + if (!raw_response.ok) return; + handle_shell_error(ip, raw_response, command, true); } export async function delete_files( @@ -159,9 +157,8 @@ export async function delete_files( command += ` && rm -r "${file_name}"`; } 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); + if (!raw_response.ok) return; + handle_shell_error(ip, raw_response, command, true); } export async function show_blackscreen(ip: string): Promise { @@ -288,11 +285,21 @@ async function request( function handle_shell_error( ip: string, - shell_response: ShellCommandResponse, + response: RequestResponse, shell_command: string, - command_includs_cd: boolean + command_includs_cd: boolean, + response_type: 'json' | 'blob' = 'json' ): boolean { - if (shell_response.exitCode !== 0) { + if ( + (response_type === 'json' && !response.json) || + (response_type === 'blob' && !response.blob) + ) { + const error_string = `Did not receive ${response_type}: ${JSON.stringify(response)}`; + console.error(error_string); + notifications.push('error', `Fehler in API-Shell`, `${ip}\n${shell_command}\n${error_string}`); + return true; + } else if (response.json && response.json.exitCode !== 0) { + const shell_response = response.json as ShellCommandResponse; if ( command_includs_cd && shell_response.stderr &&