Skip to content

Commit 9f32c5e

Browse files
add exception class for invalid modules
1 parent 9314e5f commit 9f32c5e

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

redisvl/exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class RedisVLException(Exception):
2+
"""Base RedisVL exception"""
3+
4+
5+
class RedisModuleVersionError(RedisVLException):
6+
"""Invalid module versions installed"""

redisvl/index/index.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import redis.asyncio as aredis
2626
from redis.commands.search.indexDefinition import IndexDefinition
2727

28+
from redisvl.exceptions import RedisModuleVersionError
2829
from redisvl.index.storage import BaseStorage, HashStorage, JsonStorage
2930
from redisvl.query import BaseQuery, CountQuery, FilterQuery
3031
from redisvl.query.filter import FilterExpression
@@ -354,10 +355,17 @@ def from_existing(
354355

355356
# Validate modules
356357
installed_modules = RedisConnectionFactory.get_modules(redis_client)
357-
validate_modules(
358-
installed_modules,
359-
[{"name": "search", "ver": 20810}, {"name": "searchlight", "ver": 20810}]
360-
)
358+
359+
try:
360+
required_modules = [
361+
{"name": "search", "ver": 20810},
362+
{"name": "searchlight", "ver": 20810},
363+
]
364+
validate_modules(installed_modules, required_modules)
365+
except RedisModuleVersionError as e:
366+
raise RedisModuleVersionError(
367+
f"Loading from existing index failed. {str(e)}"
368+
)
361369

362370
# Fetch index info and convert to schema
363371
index_info = cls._info(name, redis_client)
@@ -863,10 +871,16 @@ async def from_existing(
863871

864872
# Validate modules
865873
installed_modules = await RedisConnectionFactory.get_modules_async(redis_client)
866-
validate_modules(
867-
installed_modules,
868-
[{"name": "search", "ver": 20810}, {"name": "searchlight", "ver": 20810}]
869-
)
874+
try:
875+
required_modules = [
876+
{"name": "search", "ver": 20810},
877+
{"name": "searchlight", "ver": 20810},
878+
]
879+
validate_modules(installed_modules, required_modules)
880+
except RedisModuleVersionError as e:
881+
raise RedisModuleVersionError(
882+
f"Loading from existing index failed. {str(e)}"
883+
)
870884

871885
# Fetch index info and convert to schema
872886
index_info = await cls._info(name, redis_client)

redisvl/redis/connection.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from redis.connection import AbstractConnection, SSLConnection
1010
from redis.exceptions import ResponseError
1111

12+
from redisvl.exceptions import RedisModuleVersionError
1213
from redisvl.redis.constants import DEFAULT_REQUIRED_MODULES
1314
from redisvl.redis.utils import convert_bytes
1415
from redisvl.version import __version__
@@ -143,11 +144,18 @@ def validate_modules(
143144
if int(installed_version) >= int(required_module["ver"]): # type: ignore
144145
return
145146

146-
raise ValueError(
147-
f"Required Redis database module {required_module['name']} with version >= {required_module['ver']} not installed. "
148-
"See Redis Stack documentation: https://redis.io/docs/stack/"
147+
error_message = "Required Redis db module "
148+
149+
error_message += " OR ".join(
150+
[f'{module["name"]} >= {module["ver"]}' for module in required_modules]
151+
)
152+
153+
error_message += (
154+
" not installed. See Redis Stack docs at https://redis.io/docs/stack/."
149155
)
150156

157+
raise RedisModuleVersionError(error_message)
158+
151159

152160
class RedisConnectionFactory:
153161
"""Builds connections to a Redis database, supporting both synchronous and

tests/integration/test_connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from redis.asyncio import Redis as AsyncRedis
66
from redis.exceptions import ConnectionError
77

8+
from redisvl.exceptions import RedisModuleVersionError
89
from redisvl.redis.connection import (
910
RedisConnectionFactory,
1011
compare_versions,
@@ -123,7 +124,7 @@ def test_validate_modules_exist_searchlight():
123124

124125

125126
def test_validate_modules_not_exist():
126-
with pytest.raises(ValueError):
127+
with pytest.raises(RedisModuleVersionError):
127128
validate_modules(
128129
installed_modules={"search": 20811},
129130
required_modules=[

0 commit comments

Comments
 (0)