Skip to content

Commit c6ca36b

Browse files
FlauschbaellchenMarkus Richter
andauthored
Catch Redis exceptions while checking cache backend (#340)
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 <markus.richter@scale.eu>
1 parent 7fee79e commit c6ca36b

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

health_check/cache/backends.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
from health_check.exceptions import ServiceReturnedUnexpectedResult, ServiceUnavailable
66

77

8+
try:
9+
# Exceptions thrown by Redis do not subclass builtin exceptions like ConnectionError.
10+
# Additionally, not only connection errors (ConnectionError -> RedisError) can be raised,
11+
# but also errors for time-outs (TimeoutError -> RedisError)
12+
# and if the backend is read-only (ReadOnlyError -> ResponseError -> RedisError).
13+
# Since we know what we are trying to do here, we are not picky and catch the global exception RedisError.
14+
from redis.exceptions import RedisError
15+
except ModuleNotFoundError:
16+
# In case Redis is not installed and another cache backend is used.
17+
class RedisError(Exception):
18+
pass
19+
20+
821
class CacheBackend(BaseHealthCheckBackend):
922
def __init__(self, backend="default"):
1023
super().__init__()
@@ -27,5 +40,5 @@ def check_status(self):
2740
self.add_error(ServiceReturnedUnexpectedResult("Cache key warning"), e)
2841
except ValueError as e:
2942
self.add_error(ServiceReturnedUnexpectedResult("ValueError"), e)
30-
except ConnectionError as e:
43+
except (ConnectionError, RedisError) as e:
3144
self.add_error(ServiceReturnedUnexpectedResult("Connection Error"), e)

0 commit comments

Comments
 (0)