From 2a3f119d0a72354a72a9ed415d50660982ce0e5a Mon Sep 17 00:00:00 2001 From: Navin Karkera Date: Fri, 1 Sep 2023 11:27:50 +0200 Subject: [PATCH] feat: use custom exceptions for recording errors --- CHANGELOG.rst | 7 +++++++ edx_event_bus_redis/__init__.py | 2 +- edx_event_bus_redis/internal/consumer.py | 10 ++++++++-- edx_event_bus_redis/internal/producer.py | 8 ++++++-- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3078391..9d4da68 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -16,6 +16,13 @@ Unreleased * +[0.3.2] - 2023-09-01 +************************************************ + +Added +===== +* Adds custom exceptions for producing and consuming errors. + [0.3.1] - 2023-05-24 ************************************************ diff --git a/edx_event_bus_redis/__init__.py b/edx_event_bus_redis/__init__.py index a1ddc33..dc14858 100644 --- a/edx_event_bus_redis/__init__.py +++ b/edx_event_bus_redis/__init__.py @@ -5,6 +5,6 @@ from edx_event_bus_redis.internal.consumer import RedisEventConsumer from edx_event_bus_redis.internal.producer import create_producer -__version__ = '0.3.1' +__version__ = '0.3.2' default_app_config = 'edx_event_bus_redis.apps.EdxEventBusRedisConfig' # pylint: disable=invalid-name diff --git a/edx_event_bus_redis/internal/consumer.py b/edx_event_bus_redis/internal/consumer.py index 0f226e8..b968695 100644 --- a/edx_event_bus_redis/internal/consumer.py +++ b/edx_event_bus_redis/internal/consumer.py @@ -64,6 +64,12 @@ def __init__(self, message: str, causes: list): self.causes = causes # just used for testing +class EventConsumptionException(Exception): + """ + Indicates that we had an issue in event production. Useful for filtering on later. + """ + + def _reconnect_to_db_if_needed(): """ Reconnects the db connection if needed. @@ -371,8 +377,8 @@ def record_event_consuming_error(self, run_context, error, maybe_message): try: # This is gross, but our record_exception wrapper doesn't take args at the moment, # and will only read the exception from stack context. - raise Exception(error) # pylint: disable=broad-exception-raised - except BaseException: + raise EventConsumptionException(error) + except EventConsumptionException: self._add_message_monitoring(run_context=run_context, message=maybe_message, error=error) record_exception() logger.exception( diff --git a/edx_event_bus_redis/internal/producer.py b/edx_event_bus_redis/internal/producer.py index b80c6b9..49a9126 100644 --- a/edx_event_bus_redis/internal/producer.py +++ b/edx_event_bus_redis/internal/producer.py @@ -30,6 +30,10 @@ STREAM_MAX_LEN = int(getattr(settings, 'EVENT_BUS_REDIS_STREAM_MAX_LEN', 10_000)) +class EventProductionException(Exception): + """ An exception we can check for when errors occur in event production code. """ + + def record_producing_error(error, context): """ Record an error in producing an event to both the monitoring system and the regular logs @@ -41,8 +45,8 @@ def record_producing_error(error, context): try: # record_exception() is a wrapper around a New Relic method that can only be called within an except block, # so first re-raise the error - raise Exception(error) # pylint: disable=broad-exception-raised - except BaseException: + raise EventProductionException(error) + except EventProductionException: record_exception() logger.exception(f"Error delivering message to Redis event bus. {error=!s} {context!r}")