OracleDB: add exit_on_error parameter to connect method

This commit is contained in:
Benjamin Renard 2021-11-23 13:02:46 +01:00
parent d15c4f36fb
commit 8e4864f2d5

View file

@ -23,6 +23,18 @@ class OracleDBException(Exception):
super().__init__(error.format(*args, **kwargs)) super().__init__(error.format(*args, **kwargs))
class OracleDBFailToConnect(OracleDBException, RuntimeError):
"""
Raised on connecting error occurred
"""
def __init__(self, dsn, user):
super().__init__(
"An error occured during Oracle database connection ({user}@{dsn})",
user=user, dsn=dsn
)
class OracleDBDuplicatedSQLParameter(OracleDBException, KeyError): class OracleDBDuplicatedSQLParameter(OracleDBException, KeyError):
""" """
Raised when trying to set a SQL query parameter Raised when trying to set a SQL query parameter
@ -72,7 +84,7 @@ class OracleDB:
self._conn = None self._conn = None
self.just_try = just_try self.just_try = just_try
def connect(self): def connect(self, exit_on_error=True):
""" Connect to Oracle server """ """ Connect to Oracle server """
if self._conn is None: if self._conn is None:
log.info('Connect on Oracle server with DSN %s as %s', self._dsn, self._user) log.info('Connect on Oracle server with DSN %s as %s', self._dsn, self._user)
@ -82,12 +94,15 @@ class OracleDB:
password=self._pwd, password=self._pwd,
dsn=self._dsn dsn=self._dsn
) )
except Exception: except Exception as err:
log.fatal( log.fatal(
'An error occured during Oracle database connection (%s@%s).', 'An error occured during Oracle database connection (%s@%s).',
self._user, self._dsn, exc_info=1 self._user, self._dsn, exc_info=1
) )
if exit_on_error:
sys.exit(1) sys.exit(1)
else:
raise OracleDBFailToConnect(self._dsn, self._user) from err
return True return True
def close(self): def close(self):