mirror of
https://codeberg.org/PLG-Development/PLG-MuDiCS
synced 2026-07-05 16:37:09 +00:00
chore(control): improve error handling
This commit is contained in:
@@ -84,9 +84,10 @@ export async function get_file_data(
|
|||||||
`;
|
`;
|
||||||
const raw_response = await run_shell_command(ip, command);
|
const raw_response = await run_shell_command(ip, command);
|
||||||
if (!raw_response.ok || !raw_response.json) return null;
|
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;
|
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;
|
if (json_response.stdout.trim() === '') return null;
|
||||||
|
|
||||||
const response: FileInfo[] = json_response.stdout
|
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<TreeElement[] | null> {
|
export async function get_file_tree_data(ip: string, path: string): Promise<TreeElement[] | null> {
|
||||||
const command = `cd ".${path}" && tree -Js`;
|
const command = `cd ".${path}" && tree -Js`;
|
||||||
const raw_response = await run_shell_command(ip, command);
|
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;
|
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;
|
const tree_element: TreeElement | null = JSON.parse(json_response.stdout.trim())[0] || null;
|
||||||
|
|
||||||
return tree_element?.contents || null;
|
return tree_element?.contents || null;
|
||||||
@@ -130,9 +130,8 @@ export async function create_path(ip: string, path: string): Promise<void> {
|
|||||||
const command = `mkdir -p ".${path}"`;
|
const command = `mkdir -p ".${path}"`;
|
||||||
|
|
||||||
const raw_response = await run_shell_command(ip, command);
|
const raw_response = await run_shell_command(ip, command);
|
||||||
if (!raw_response.ok || !raw_response.json) return;
|
if (!raw_response.ok) return;
|
||||||
const json_response = raw_response.json as ShellCommandResponse;
|
handle_shell_error(ip, raw_response, command, true);
|
||||||
handle_shell_error(ip, json_response, command, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function rename_file(
|
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 command: string = `cd ".${path}" && mv "${old_file_name}" "${new_file_name}"`;
|
||||||
|
|
||||||
const raw_response = await run_shell_command(ip, command);
|
const raw_response = await run_shell_command(ip, command);
|
||||||
if (!raw_response.ok || !raw_response.json) return;
|
if (!raw_response.ok) return;
|
||||||
const json_response = raw_response.json as ShellCommandResponse;
|
handle_shell_error(ip, raw_response, command, true);
|
||||||
handle_shell_error(ip, json_response, command, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function delete_files(
|
export async function delete_files(
|
||||||
@@ -159,9 +157,8 @@ export async function delete_files(
|
|||||||
command += ` && rm -r "${file_name}"`;
|
command += ` && rm -r "${file_name}"`;
|
||||||
}
|
}
|
||||||
const raw_response = await run_shell_command(ip, command);
|
const raw_response = await run_shell_command(ip, command);
|
||||||
if (!raw_response.ok || !raw_response.json) return;
|
if (!raw_response.ok) return;
|
||||||
const json_response = raw_response.json as ShellCommandResponse;
|
handle_shell_error(ip, raw_response, command, true);
|
||||||
handle_shell_error(ip, json_response, command, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function show_blackscreen(ip: string): Promise<void> {
|
export async function show_blackscreen(ip: string): Promise<void> {
|
||||||
@@ -288,11 +285,21 @@ async function request(
|
|||||||
|
|
||||||
function handle_shell_error(
|
function handle_shell_error(
|
||||||
ip: string,
|
ip: string,
|
||||||
shell_response: ShellCommandResponse,
|
response: RequestResponse,
|
||||||
shell_command: string,
|
shell_command: string,
|
||||||
command_includs_cd: boolean
|
command_includs_cd: boolean,
|
||||||
|
response_type: 'json' | 'blob' = 'json'
|
||||||
): boolean {
|
): 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 (
|
if (
|
||||||
command_includs_cd &&
|
command_includs_cd &&
|
||||||
shell_response.stderr &&
|
shell_response.stderr &&
|
||||||
|
|||||||
Reference in New Issue
Block a user