From c6e1e662ad488bb4a5017c073195bf5a2e043ea4 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Wed, 3 Sep 2025 10:33:27 -0700 Subject: [PATCH] improve error handling --- notifications_utils/clients/redis/redis_client.py | 2 ++ notifications_utils/recipients.py | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/notifications_utils/clients/redis/redis_client.py b/notifications_utils/clients/redis/redis_client.py index d96f967a2..b7669773c 100644 --- a/notifications_utils/clients/redis/redis_client.py +++ b/notifications_utils/clients/redis/redis_client.py @@ -80,6 +80,7 @@ class RedisClient: try: return self.scripts["delete-keys-by-pattern"](args=[pattern]) except Exception as e: + current_app.logger.exception(f"Exception in delete_by_pattern pattern={pattern}") self.__handle_exception( e, raise_exception, "delete-by-pattern", pattern ) @@ -129,6 +130,7 @@ class RedisClient: result = pipe.execute() return result[2] > limit except Exception as e: + current_app.logger.exception(f"Exception in exceeded_rate_limit cache_key {cache_key} limit = {limit}") self.__handle_exception( e, raise_exception, "rate-limit-pipeline", cache_key ) diff --git a/notifications_utils/recipients.py b/notifications_utils/recipients.py index 867c719f9..d41419064 100644 --- a/notifications_utils/recipients.py +++ b/notifications_utils/recipients.py @@ -83,7 +83,8 @@ class RecipientCSV: def guestlist(self, value): try: self._guestlist = list(value) - except TypeError: + except TypeError as te: + current_app.logger.exception(f"Type error setting the guest list to {value}") self._guestlist = [] @property @@ -111,6 +112,7 @@ class RecipientCSV: try: self._placeholders = list(value) + self.recipient_column_headers except TypeError: + current_app.logger.exception(f"TypeError setting the placeholders to {value}") self._placeholders = self.recipient_column_headers self.placeholders_as_column_keys = [ InsensitiveDict.make_key(placeholder) for placeholder in self._placeholders @@ -350,6 +352,7 @@ class RecipientCSV: value, international=self.allow_international_sms ) except (InvalidEmailError, InvalidPhoneError) as error: + current_app.logger.exception(f"Email or phone error for {value}") return str(error) if InsensitiveDict.make_key(key) not in self.placeholders_as_column_keys: @@ -511,6 +514,7 @@ def is_us_phone_number(number): try: return _get_country_code(number) == us_prefix except NumberParseException: + current_app.logger.exception("NumberParseException checking if phone number is US number") return False @@ -599,6 +603,7 @@ def validate_us_phone_number(number): raise InvalidPhoneError("Phone number range is not in use") raise InvalidPhoneError("Phone number is not possible") except NumberParseException as exc: + current_app.logger.exception("NumberParseException validating US phone number") raise InvalidPhoneError(exc._msg) from exc @@ -655,6 +660,7 @@ def try_validate_and_format_phone_number(number, international=None, log_msg=Non try: return validate_and_format_phone_number(number, international) except InvalidPhoneError as exc: + current_app.logger.exception("InvalidPhoneNumber while trying to validate and format phone number") if log_msg: current_app.logger.warning("{}: {}".format(log_msg, exc)) return number @@ -689,6 +695,7 @@ def validate_email_address(email_address): # noqa (C901 too complex) try: hostname = hostname.encode("idna").decode("ascii") except UnicodeError: + current_app.logger.exception("UnicodeError validating email address") raise InvalidEmailError parts = hostname.split(".") @@ -731,6 +738,7 @@ def format_phone_number_human_readable(phone_number): phone_number = validate_phone_number(phone_number, international=True) except InvalidPhoneError: # if there was a validation error, we want to shortcut out here, but still display the number on the front end + current_app.logger.exception("InvalidPhoneError trying to format_phone_number_human_readable()") return phone_number international_phone_info = get_international_phone_info(phone_number)