[+] 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 3159fed81e
3 changed files with 16 additions and 9 deletions

View File

@ -2,7 +2,6 @@ import logging
import os
import shutil
import re
import subprocess
supported_versions = ["bukkit", "paper", "spigot"]

View File

@ -45,7 +45,7 @@ def fetch_port() -> int or None:
return None
def user_field_exists(user_id: str, field: str) -> bool:
def user_field_exists(user_id: str) -> bool:
try:
doc = firestore_database.collection('users').document(user_id).get()
if doc.exists:
@ -79,7 +79,8 @@ def get_server_port(user_id: str) -> int or None:
server_doc = servers_ref.get()
port = server_doc.get('port')
return port if port else None
except Exception:
except Exception as e:
file_manager.log_error(type(e).__name__, str(e))
return None

View File

@ -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)}"