[+] Error handling & firebase logs

Signed-off-by: Charles Le Maux <charles.le-maux@epitech.eu>
This commit is contained in:
Charles Le Maux 2024-07-11 14:45:13 +01:00
parent f9534c6aa2
commit e60e972ebd

View File

@ -51,7 +51,8 @@ def fetch_servers(user: UserRecord) -> tuple[HTTPStatus, str or list]:
if not servers_data: if not servers_data:
account_create(user) account_create(user)
return HTTPStatus.OK, servers_data return HTTPStatus.OK, servers_data
except Exception: except Exception as e:
file_manager.log_error(type(e).__name__, str(e))
return account_create(user) return account_create(user)
@ -69,6 +70,7 @@ def account_create(user: UserRecord) -> tuple[HTTPStatus, str or None]:
}) })
return HTTPStatus.CREATED, "Successfully created account." return HTTPStatus.CREATED, "Successfully created account."
except Exception as e: except Exception as e:
file_manager.log_error(type(e).__name__, str(e))
return HTTPStatus.INTERNAL_SERVER_ERROR, "Error creating account." 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) file_manager.update_server_property(server_path + "/server.properties", "server-port", port)
return HTTPStatus.CREATED, f"Successfully created server '{name}'." return HTTPStatus.CREATED, f"Successfully created server '{name}'."
except Exception as e: except Exception as e:
file_manager.log_error(type(e).__name__, str(e))
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Server creation failed | {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) firebase_manager.delete_server(user_id, name)
file_manager.delete_non_empty_folder(server_path) file_manager.delete_non_empty_folder(server_path)
return HTTPStatus.OK, f"Successfully deleted server {name}." 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 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 user_id = user.uid
try: try:
dns_record_id = firebase_manager.get_user_field(user_id, "cloudflare_entry") 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: try:
firebase_manager.update_server_running_state(user_id, name, True) firebase_manager.update_server_running_state(user_id, name, True)
except Exception as e: 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}." return HTTPStatus.ACCEPTED, f"Successfully started server {name}."
except Exception as e: 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}" 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"Successfully stopped server {name}."
return HTTPStatus.OK, f"Server {name} already stopped." return HTTPStatus.OK, f"Server {name} already stopped."
except Exception as e: except Exception as e:
file_manager.log_error(type(e).__name__, str(e))
if port: if port:
mc_manager.stop_server_forcefully(port) mc_manager.stop_server_forcefully(port)
return HTTPStatus.OK, f"Successfully stopped server {name}." 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: except FileNotFoundError:
return HTTPStatus.NOT_FOUND, f"File server.properties for server '{name}' not found." return HTTPStatus.NOT_FOUND, f"File server.properties for server '{name}' not found."
except Exception as e: 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)}" 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) mc_manager.execute_server_command(port, command)
return HTTPStatus.OK, f"Command '{command}' executed successfully." return HTTPStatus.OK, f"Command '{command}' executed successfully."
except Exception as e: 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)}" return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error executing command: {command} || {str(e)}"