22import sys
33from argparse import Namespace
44
5+ import yaml
6+ from pydantic import ValidationError
7+
58from redisvl .cli .utils import add_index_parsing_options , create_redis_url
9+ from redisvl .exceptions import RedisSearchError
610from redisvl .index import SearchIndex
711from redisvl .redis .connection import RedisConnectionFactory
812from redisvl .redis .utils import convert_bytes , make_dict
1115
1216logger = get_logger ("[RedisVL]" )
1317
18+ # Exceptions commonly raised when loading or validating a schema path (-s).
19+ SCHEMA_INPUT_ERRORS = (
20+ FileNotFoundError ,
21+ ValueError ,
22+ yaml .YAMLError ,
23+ ValidationError ,
24+ )
25+
26+
27+ def exit_schema_input_error (args : Namespace , exc : BaseException ) -> None :
28+ if not args .schema :
29+ raise exc
30+ print (str (exc ), file = sys .stderr )
31+ sys .exit (2 )
32+
33+
34+ def exit_redis_search_error (
35+ args : Namespace , index : SearchIndex | None , exc : RedisSearchError
36+ ) -> None :
37+ name = (
38+ index .schema .index .name
39+ if index is not None
40+ else (args .index or args .schema or "unknown" )
41+ )
42+ print (
43+ f"Redis search operation failed for index { name !r} . { exc } " ,
44+ file = sys .stderr ,
45+ )
46+ sys .exit (1 )
47+
1448
1549class Index :
1650 usage = "\n " .join (
@@ -33,26 +67,38 @@ def __init__(self):
3367 parser = add_index_parsing_options (parser )
3468
3569 args = parser .parse_args (sys .argv [2 :])
70+
3671 if not hasattr (self , args .command ):
37- parser .print_help ()
38- exit (0 )
72+ print (f"Unknown command: { args .command } \n " , file = sys .stderr )
73+ parser .print_help (sys .stderr )
74+ sys .exit (2 )
75+
3976 try :
4077 getattr (self , args .command )(args )
4178 except Exception as e :
42- logger .error (e )
43- exit (0 )
79+ logger .error (e , exc_info = True )
80+ print (str (e ), file = sys .stderr )
81+ sys .exit (1 )
4482
4583 def create (self , args : Namespace ):
4684 """Create an index.
4785
4886 Usage:
49- rvl index create -i <index_name> | - s <schema_path>
87+ rvl index create -s <schema_path>
5088 """
5189 if not args .schema :
52- print ("Schema must be provided to create an index" )
53- exit (1 )
54- index = SearchIndex .from_yaml (args .schema , redis_url = create_redis_url (args ))
55- index .create ()
90+ print ("Schema must be provided to create an index" , file = sys .stderr )
91+ sys .exit (2 )
92+
93+ redis_url = create_redis_url (args )
94+ try :
95+ index = SearchIndex .from_yaml (args .schema , redis_url = redis_url )
96+ except SCHEMA_INPUT_ERRORS as e :
97+ exit_schema_input_error (args , e )
98+ try :
99+ index .create ()
100+ except RedisSearchError as e :
101+ exit_redis_search_error (args , index , e )
56102 print ("Index created successfully" )
57103
58104 def info (self , args : Namespace ):
@@ -62,7 +108,10 @@ def info(self, args: Namespace):
62108 rvl index info -i <index_name> | -s <schema_path>
63109 """
64110 index = self ._connect_to_index (args )
65- _display_in_table (index .info ())
111+ try :
112+ _display_in_table (index .info ())
113+ except RedisSearchError as e :
114+ exit_redis_search_error (args , index , e )
66115
67116 def listall (self , args : Namespace ):
68117 """List all indices.
@@ -84,7 +133,10 @@ def delete(self, args: Namespace, drop=False):
84133 rvl index delete -i <index_name> | -s <schema_path>
85134 """
86135 index = self ._connect_to_index (args )
87- index .delete (drop = drop )
136+ try :
137+ index .delete (drop = drop )
138+ except RedisSearchError as e :
139+ exit_redis_search_error (args , index , e )
88140 print ("Index deleted successfully" )
89141
90142 def destroy (self , args : Namespace ):
@@ -96,19 +148,23 @@ def destroy(self, args: Namespace):
96148 self .delete (args , drop = True )
97149
98150 def _connect_to_index (self , args : Namespace ) -> SearchIndex :
99- # connect to redis
100151 redis_url = create_redis_url (args )
101152
102153 if args .index :
103- schema = IndexSchema .from_dict ({"index" : {"name" : args .index }})
104- index = SearchIndex (schema = schema , redis_url = redis_url )
105- elif args .schema :
106- index = SearchIndex .from_yaml (args .schema , redis_url = redis_url )
107- else :
108- print ("Index name or schema must be provided" )
109- exit (1 )
110-
111- return index
154+ try :
155+ schema = IndexSchema .from_dict ({"index" : {"name" : args .index }})
156+ return SearchIndex (schema = schema , redis_url = redis_url )
157+ except SCHEMA_INPUT_ERRORS as e :
158+ exit_schema_input_error (args , e )
159+
160+ if args .schema :
161+ try :
162+ return SearchIndex .from_yaml (args .schema , redis_url = redis_url )
163+ except SCHEMA_INPUT_ERRORS as e :
164+ exit_schema_input_error (args , e )
165+
166+ print ("Index name or schema must be provided" , file = sys .stderr )
167+ sys .exit (2 )
112168
113169
114170def _display_in_table (index_info ):
0 commit comments