#2345: remove try catch + todo - figure out why db connection cannot be made

This commit is contained in:
Czar Echavez
2024-09-05 16:47:17 +01:00
parent 1234b2dc4a
commit a998b8e22b

View File

@@ -108,9 +108,9 @@ class WebServer(Service):
:type: payload: HttpRequestPacket :type: payload: HttpRequestPacket
""" """
response = HttpResponsePacket(status_code=HttpStatusCode.NOT_FOUND, payload=payload) response = HttpResponsePacket(status_code=HttpStatusCode.NOT_FOUND, payload=payload)
try:
parsed_url = urlparse(payload.request_url) parsed_url = urlparse(payload.request_url)
path = parsed_url.path.strip("/") path = parsed_url.path.strip("/") if parsed_url and parsed_url.path else ""
if len(path) < 1: if len(path) < 1:
# query succeeded # query succeeded
@@ -119,8 +119,9 @@ class WebServer(Service):
if path.startswith("users"): if path.startswith("users"):
# get data from DatabaseServer # get data from DatabaseServer
# get all users # get all users
if not self.db_connection: if self._establish_db_connection():
self._establish_db_connection() # unable to create a db connection
response.status_code = HttpStatusCode.INTERNAL_SERVER_ERROR
if self.db_connection.query("SELECT"): if self.db_connection.query("SELECT"):
# query succeeded # query succeeded
@@ -128,17 +129,22 @@ class WebServer(Service):
response.status_code = HttpStatusCode.OK response.status_code = HttpStatusCode.OK
else: else:
self.set_health_state(SoftwareHealthState.COMPROMISED) self.set_health_state(SoftwareHealthState.COMPROMISED)
return response
except Exception: # TODO: refactor this. Likely to cause silent bugs. (ADO ticket #2345 )
# something went wrong on the server
response.status_code = HttpStatusCode.INTERNAL_SERVER_ERROR
return response return response
def _establish_db_connection(self) -> None: def _establish_db_connection(self) -> bool:
"""Establish a connection to db.""" """Establish a connection to db."""
# if active db connection, return true
if self.db_connection:
return True
# otherwise, try to create db connection
db_client = self.software_manager.software.get("DatabaseClient") db_client = self.software_manager.software.get("DatabaseClient")
if db_client is None:
return False # database client not installed
self.db_connection: DatabaseClientConnection = db_client.get_new_connection() self.db_connection: DatabaseClientConnection = db_client.get_new_connection()
return self.db_connection is not None
def send( def send(
self, self,