11from fastapi import FastAPI
22from fastapi .middleware .cors import CORSMiddleware
33from src .routers import movies
4- from src .utils .errorHandler import register_error_handlers
4+ from src .utils .errorHandler import register_error_handlers , create_error_response
55from src .database .mongo_client import db , get_collection
66import traceback
77import os
@@ -34,7 +34,6 @@ async def ensure_search_index():
3434 indexes = [idx async for idx in result ]
3535 index_names = [index ["name" ] for index in indexes ]
3636 if "movieSearchIndex" in index_names :
37- print ("MongoDB Search index already exists." )
3837 return
3938
4039 # Create a mapping if the movieSearchIndex does not exist
@@ -58,9 +57,12 @@ async def ensure_search_index():
5857 "definition" : index_definition
5958 }]
6059 })
61- print ("MongoDB Search index created." )
6260 except Exception as e :
63- print (f"Error creating the search index: { e } " )
61+ raise RuntimeError (
62+ f"Failed to create search index 'movieSearchIndex': { str (e )} . "
63+ f"Search functionality may not work properly. "
64+ f"Please check your MongoDB Atlas configuration and ensure the cluster supports search indexes."
65+ )
6466
6567@app .on_event ("startup" )
6668async def vector_search_index ():
@@ -69,7 +71,6 @@ async def vector_search_index():
6971 This ensures the index is ready before any vector search requests are made.
7072 """
7173 try :
72-
7374 embedded_movies_collection = get_collection ("embedded_movies" )
7475
7576 # Get list of existing indexes - convert AsyncCommandCursor to list
@@ -98,9 +99,12 @@ async def vector_search_index():
9899
99100 # Create the index
100101 result = await embedded_movies_collection .create_search_index (index_definition )
101- print ("Vector search index 'vector_index' ready to query." )
102102
103103 except Exception as e :
104- print (f"Error during vector search index setup: { str (e )} " )
105- print (f"Error type: { type (e ).__name__ } " )
104+ raise RuntimeError (
105+ f"Failed to create vector search index 'vector_index': { str (e )} . "
106+ f"Vector search functionality will not be available. "
107+ f"Please check your MongoDB Atlas configuration, ensure the cluster supports vector search, "
108+ f"and verify the 'embedded_movies' collection exists with the required embedding field."
109+ )
106110
0 commit comments