diff --git a/src/backend/history/cosmosdbservice.py b/src/backend/history/cosmosdbservice.py index 9add46ea2..c245a2a37 100644 --- a/src/backend/history/cosmosdbservice.py +++ b/src/backend/history/cosmosdbservice.py @@ -25,9 +25,10 @@ def __init__( ) except exceptions.CosmosHttpResponseError as e: if e.status_code == 401: - raise ValueError("Invalid credentials") from e + raise ValueError("Unauthorized") from e else: - raise ValueError("Invalid CosmosDB endpoint") from e + logging.error("Cosmos error: %s", e) + raise ValueError("Something went wrong!") from e try: self.database_client = self.cosmosdb_client.get_database_client( @@ -35,6 +36,9 @@ def __init__( ) except exceptions.CosmosResourceNotFoundError: raise ValueError("Invalid CosmosDB database name") + except Exception as e: + logging.error("Unexpected error while accessing CosmosDB database: %s", e) + raise try: self.container_client = self.database_client.get_container_client( @@ -42,6 +46,8 @@ def __init__( ) except exceptions.CosmosResourceNotFoundError: raise ValueError("Invalid CosmosDB container name") + except: + pass async def ensure(self): if ( @@ -49,7 +55,7 @@ async def ensure(self): or not self.database_client or not self.container_client ): - return False, "CosmosDB client not initialized correctly" + return False, "client not initialized" try: await self.database_client.read() except Exception: @@ -73,7 +79,13 @@ async def create_conversation(self, user_id, title=""): "updatedAt": datetime.utcnow().isoformat(), "userId": user_id, "title": title, + # Metadata field for storing additional information about the conversation. + # This field is currently an empty dictionary but can be extended in the future. + # Metadata field for storing additional information about the conversation. + # This field is currently an empty dictionary but can be extended in the future. + "metadata": {} } + validate_metadata(conversation["metadata"]) # TODO: add some error handling based on the output of the upsert_item call resp = await self.container_client.upsert_item(conversation) if resp: