diff options
-rw-r--r-- | oslo_db/sqlalchemy/exc_filters.py | 4 | ||||
-rw-r--r-- | oslo_db/tests/sqlalchemy/test_exc_filters.py | 33 |
2 files changed, 35 insertions, 2 deletions
diff --git a/oslo_db/sqlalchemy/exc_filters.py b/oslo_db/sqlalchemy/exc_filters.py index 9803343..e42718b 100644 --- a/oslo_db/sqlalchemy/exc_filters.py +++ b/oslo_db/sqlalchemy/exc_filters.py @@ -475,11 +475,11 @@ def handler(context): if isinstance( dbe, exception.DBConnectionError): context.is_disconnect = True - raise + return dbe def register_engine(engine): - event.listen(engine, "handle_error", handler) + event.listen(engine, "handle_error", handler, retval=True) @event.listens_for(engine, "rollback_savepoint") def rollback_savepoint(conn, name, context): diff --git a/oslo_db/tests/sqlalchemy/test_exc_filters.py b/oslo_db/tests/sqlalchemy/test_exc_filters.py index ce148db..93ad770 100644 --- a/oslo_db/tests/sqlalchemy/test_exc_filters.py +++ b/oslo_db/tests/sqlalchemy/test_exc_filters.py @@ -1496,3 +1496,36 @@ class TestDBConnectPingWrapping(TestsExceptionFilter): self.OperationalError('%d MySQL server has gone away' % code), is_disconnect=False ) + + +class TestsErrorHandler(TestsExceptionFilter): + def test_multiple_error_handlers(self): + handler = mock.MagicMock(return_value=None) + sqla.event.listen(self.engine, "handle_error", handler, retval=True) + + # cause an error in DB API + self._run_test( + "mysql", "select you_made_a_programming_error", + self.ProgrammingError("Error 123, you made a mistake"), + exception.DBError + ) + + # expect custom handler to be called together with oslo.db's one + self.assertEqual(1, handler.call_count, + 'Custom handler should be called') + + def test_chained_exceptions(self): + class CustomError(Exception): + pass + + def handler(context): + return CustomError('Custom Error') + + sqla.event.listen(self.engine, "handle_error", handler, retval=True) + + # cause an error in DB API, expect exception from custom handler + self._run_test( + "mysql", "select you_made_a_programming_error", + self.ProgrammingError("Error 123, you made a mistake"), + CustomError + ) |