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

## Summary
Remove try catch in webserver to prevent difficult to debug problems

## Test process
Used existing tests - this is because webserver should still act as before - only the try catch block is removed

## Checklist
- [X] PR is linked to a **work item**
- [X] **acceptance criteria** of linked ticket are met
- [X] performed **self-review** of the code
- [ ] written **tests** for any new functionality added with this PR
- [ ] updated the **documentation** if this PR changes or adds functionality
- [ ] written/updated **design docs** if this PR implements new functionality
- [ ] updated the **change log**
- [X] ran **pre-commit** checks for code style
- [ ] attended to any **TO-DOs** left in the code

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

Related work items: #2345
This commit is contained in:
Czar Echavez
2024-09-10 12:39:18 +00:00

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,10 @@ 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 not self._establish_db_connection():
self._establish_db_connection() # unable to create a db connection
response.status_code = HttpStatusCode.INTERNAL_SERVER_ERROR
return response
if self.db_connection.query("SELECT"): if self.db_connection.query("SELECT"):
# query succeeded # query succeeded
@@ -128,17 +130,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,