Added error handling to DBContext

This commit is contained in:
Sven Heidemann 2022-07-19 14:23:17 +02:00
parent 4bcab95369
commit a3512b56d7

View File

@ -6,9 +6,9 @@ from cpl_core.logging import LoggerABC
class DBContext(DatabaseContext): class DBContext(DatabaseContext):
def __init__(self, logger: LoggerABC): def __init__(self, logger: LoggerABC):
self._logger = logger self._logger = logger
DatabaseContext.__init__(self) DatabaseContext.__init__(self)
def connect(self, database_settings: DatabaseSettings): def connect(self, database_settings: DatabaseSettings):
@ -20,7 +20,7 @@ class DBContext(DatabaseContext):
self._logger.info(__name__, "Connected to database") self._logger.info(__name__, "Connected to database")
except Exception as e: except Exception as e:
self._logger.fatal(__name__, "Connecting to database failed", e) self._logger.fatal(__name__, "Connecting to database failed", e)
def save_changes(self): def save_changes(self):
try: try:
self._logger.trace(__name__, "Save changes") self._logger.trace(__name__, "Save changes")
@ -28,6 +28,10 @@ class DBContext(DatabaseContext):
self._logger.debug(__name__, "Saved changes") self._logger.debug(__name__, "Saved changes")
except Exception as e: except Exception as e:
self._logger.error(__name__, "Saving changes failed", e) self._logger.error(__name__, "Saving changes failed", e)
def select(self, statement: str) -> list[tuple]: def select(self, statement: str) -> list[tuple]:
return super(DBContext, self).select(statement) try:
return super(DBContext, self).select(statement)
except Exception as e:
self._logger.error(__name__, f'Database error caused by {statement}', e)
return []