From 7c8369a31627ba919231ade0eafaeecffd218554 Mon Sep 17 00:00:00 2001 From: checktheroads Date: Tue, 28 Jul 2020 09:49:13 -0700 Subject: [PATCH] closes #56 - fix redis connection error due to invalid host type --- hyperglass/cache/aio.py | 19 ++++++++++++------- hyperglass/cache/base.py | 2 +- hyperglass/cache/sync.py | 19 ++++++++++++------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/hyperglass/cache/aio.py b/hyperglass/cache/aio.py index 9691e6a..56d9e00 100644 --- a/hyperglass/cache/aio.py +++ b/hyperglass/cache/aio.py @@ -10,9 +10,11 @@ from typing import Any, Dict # Third Party from aredis import StrictRedis as AsyncRedis from aredis.pubsub import PubSub as AsyncPubSub +from aredis.exceptions import RedisError # Project from hyperglass.cache.base import BaseCache +from hyperglass.exceptions import HyperglassError class AsyncCache(BaseCache): @@ -21,13 +23,16 @@ class AsyncCache(BaseCache): def __init__(self, *args, **kwargs): """Initialize Redis connection.""" super().__init__(*args, **kwargs) - self.instance: AsyncRedis = AsyncRedis( - db=self.db, - host=self.host, - port=self.port, - decode_responses=self.decode_responses, - **self.redis_args, - ) + try: + self.instance: AsyncRedis = AsyncRedis( + db=self.db, + host=self.host, + port=self.port, + decode_responses=self.decode_responses, + **self.redis_args, + ) + except RedisError as err: + raise HyperglassError(str(err), level="danger") async def get(self, *args: str) -> Any: """Get item(s) from cache.""" diff --git a/hyperglass/cache/base.py b/hyperglass/cache/base.py index 66ed278..e1f6869 100644 --- a/hyperglass/cache/base.py +++ b/hyperglass/cache/base.py @@ -19,7 +19,7 @@ class BaseCache: ) -> None: """Initialize Redis connection.""" self.db: int = db - self.host: str = host + self.host: str = str(host) self.port: int = port self.decode_responses: bool = decode_responses self.redis_args: dict = kwargs diff --git a/hyperglass/cache/sync.py b/hyperglass/cache/sync.py index 5905a2f..88d4af1 100644 --- a/hyperglass/cache/sync.py +++ b/hyperglass/cache/sync.py @@ -9,9 +9,11 @@ from typing import Any, Dict # Third Party from redis import Redis as SyncRedis from redis.client import PubSub as SyncPubsSub +from redis.exceptions import RedisError # Project from hyperglass.cache.base import BaseCache +from hyperglass.exceptions import HyperglassError class SyncCache(BaseCache): @@ -20,13 +22,16 @@ class SyncCache(BaseCache): def __init__(self, *args, **kwargs): """Initialize Redis connection.""" super().__init__(*args, **kwargs) - self.instance: SyncRedis = SyncRedis( - db=self.db, - host=self.host, - port=self.port, - decode_responses=self.decode_responses, - **self.redis_args, - ) + try: + self.instance: SyncRedis = SyncRedis( + db=self.db, + host=self.host, + port=self.port, + decode_responses=self.decode_responses, + **self.redis_args, + ) + except RedisError as err: + raise HyperglassError(str(err), level="danger") def get(self, *args: str) -> Any: """Get item(s) from cache."""