Skip to content

Commit

Permalink
Catch Redis exceptions while checking cache backend (#340)
Browse files Browse the repository at this point in the history
Exceptions thrown by Redis do not subclass builtin exceptions like ConnectionError.
Prior to this commit such exceptions would not be catched while running
the health-check on redis-backed cache backends.

Co-authored-by: Markus Richter <[email protected]>
  • Loading branch information
Flauschbaellchen and Markus Richter committed Jun 5, 2023
1 parent 7fee79e commit c6ca36b
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion health_check/cache/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
from health_check.exceptions import ServiceReturnedUnexpectedResult, ServiceUnavailable


try:
# Exceptions thrown by Redis do not subclass builtin exceptions like ConnectionError.
# Additionally, not only connection errors (ConnectionError -> RedisError) can be raised,
# but also errors for time-outs (TimeoutError -> RedisError)
# and if the backend is read-only (ReadOnlyError -> ResponseError -> RedisError).
# Since we know what we are trying to do here, we are not picky and catch the global exception RedisError.
from redis.exceptions import RedisError
except ModuleNotFoundError:
# In case Redis is not installed and another cache backend is used.
class RedisError(Exception):
pass


class CacheBackend(BaseHealthCheckBackend):
def __init__(self, backend="default"):
super().__init__()
Expand All @@ -27,5 +40,5 @@ def check_status(self):
self.add_error(ServiceReturnedUnexpectedResult("Cache key warning"), e)
except ValueError as e:
self.add_error(ServiceReturnedUnexpectedResult("ValueError"), e)
except ConnectionError as e:
except (ConnectionError, RedisError) as e:
self.add_error(ServiceReturnedUnexpectedResult("Connection Error"), e)

0 comments on commit c6ca36b

Please sign in to comment.