From e60e972ebdc2921f061ee60b632dfea2c91297ad Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Thu, 11 Jul 2024 14:45:13 +0100 Subject: [PATCH] [+] Error handling & firebase logs Signed-off-by: Charles Le Maux --- generic_executor.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/generic_executor.py b/generic_executor.py index 9bc1de2..1e7beb8 100644 --- a/generic_executor.py +++ b/generic_executor.py @@ -51,7 +51,8 @@ def fetch_servers(user: UserRecord) -> tuple[HTTPStatus, str or list]: if not servers_data: account_create(user) return HTTPStatus.OK, servers_data - except Exception: + except Exception as e: + file_manager.log_error(type(e).__name__, str(e)) return account_create(user) @@ -69,6 +70,7 @@ def account_create(user: UserRecord) -> tuple[HTTPStatus, str or None]: }) return HTTPStatus.CREATED, "Successfully created account." except Exception as e: + file_manager.log_error(type(e).__name__, str(e)) return HTTPStatus.INTERNAL_SERVER_ERROR, "Error creating account." @@ -99,6 +101,7 @@ def server_create(user: UserRecord, name: str, version: str, framework: str = "p file_manager.update_server_property(server_path + "/server.properties", "server-port", port) return HTTPStatus.CREATED, f"Successfully created server '{name}'." except Exception as e: + file_manager.log_error(type(e).__name__, str(e)) return HTTPStatus.INTERNAL_SERVER_ERROR, f"Server creation failed | {e}" @@ -109,11 +112,12 @@ def server_delete(name: str, user: UserRecord) -> tuple[HTTPStatus, str or None] firebase_manager.delete_server(user_id, name) file_manager.delete_non_empty_folder(server_path) return HTTPStatus.OK, f"Successfully deleted server {name}." - except Exception: + except Exception as e: + file_manager.log_error(type(e).__name__, str(e)) return HTTPStatus.INTERNAL_SERVER_ERROR, None -def account_delete(user: UserRecord, subdomain: str) -> tuple[HTTPStatus, str or None]: +def account_delete(user: UserRecord) -> tuple[HTTPStatus, str or None]: user_id = user.uid try: dns_record_id = firebase_manager.get_user_field(user_id, "cloudflare_entry") @@ -145,10 +149,11 @@ def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]: try: firebase_manager.update_server_running_state(user_id, name, True) except Exception as e: - return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error updating server {name}'s running state in database. {type(e).__name__}{str(e)}." + return HTTPStatus.INTERNAL_SERVER_ERROR, (f"Error updating server {name}'s running state in database. " + f"{type(e).__name__}{str(e)}.") return HTTPStatus.ACCEPTED, f"Successfully started server {name}." except Exception as e: - print(f"Error when running server: {e}") + file_manager.log_error(type(e).__name__, str(e)) return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error when running server: {e}" @@ -164,6 +169,7 @@ def server_stop(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]: return HTTPStatus.OK, f"Successfully stopped server {name}." return HTTPStatus.OK, f"Server {name} already stopped." except Exception as e: + file_manager.log_error(type(e).__name__, str(e)) if port: mc_manager.stop_server_forcefully(port) return HTTPStatus.OK, f"Successfully stopped server {name}." @@ -182,6 +188,7 @@ def update_property(uid: str, name: str, prop: str, value: str) -> tuple[HTTPSta except FileNotFoundError: return HTTPStatus.NOT_FOUND, f"File server.properties for server '{name}' not found." except Exception as e: + file_manager.log_error(type(e).__name__, str(e)) return HTTPStatus.INTERNAL_SERVER_ERROR, f"Unhandled error: {type(e).__name__}, {str(e)}" @@ -206,7 +213,7 @@ def run_command(user: UserRecord, command: str, name: str) -> tuple[HTTPStatus, mc_manager.execute_server_command(port, command) return HTTPStatus.OK, f"Command '{command}' executed successfully." except Exception as e: - print(f"Error executing command: {e}") + file_manager.log_error(type(e).__name__, str(e)) return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error executing command: {command} || {str(e)}"