Skip to content
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/backend/history/cosmosdbservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,36 @@ 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(
database_name
)
except exceptions.CosmosResourceNotFoundError:
raise ValueError("Invalid CosmosDB database name")
except:
pass
Copy link
Preview

Copilot AI May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid bare except clauses, as they can hide unexpected errors; catch specific exceptions or log the exception before suppressing it.

Suggested change
except:
pass
except Exception as e:
import logging
logging.error(f"Unexpected error while accessing CosmosDB database: {e}")
raise

Copilot uses AI. Check for mistakes.


try:
self.container_client = self.database_client.get_container_client(
container_name
)
except exceptions.CosmosResourceNotFoundError:
raise ValueError("Invalid CosmosDB container name")
except:
pass
Comment on lines +49 to +50
Copy link
Preview

Copilot AI May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid bare except clauses here as well; handle only expected exceptions or at minimum log unexpected errors to aid troubleshooting.

Suggested change
except:
pass
except Exception as e:
import logging
logging.error(f"Unexpected error while getting container client: {e}")
raise

Copilot uses AI. Check for mistakes.

Comment on lines +49 to +50
Copy link
Preview

Copilot AI May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using a bare except block that swallows exceptions without logging; consider catching specific exceptions to better handle error scenarios.

Suggested change
except:
pass
except exceptions.CosmosHttpResponseError as e:
logging.error("Unexpected CosmosDB error while accessing container: %s", e)
raise

Copilot uses AI. Check for mistakes.

Comment on lines +49 to +50
Copy link
Preview

Copilot AI May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a bare 'except:' block here may silently mask errors during container client initialization. Consider catching 'Exception as e' and logging the error to aid in debugging.

Suggested change
except:
pass
except Exception as e:
logging.error("Unexpected error while accessing CosmosDB container: %s", e)
raise

Copilot uses AI. Check for mistakes.


async def ensure(self):
if (
not self.cosmosdb_client
or not self.database_client
or not self.container_client
):
return False, "CosmosDB client not initialized correctly"
return False, "client not initialized"
Comment on lines 53 to +58
Copy link
Preview

Copilot AI May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message 'client not initialized' is less descriptive than the previous 'CosmosDB client not initialized correctly'; consider restoring clarity about which client failed initialization.

Copilot uses AI. Check for mistakes.

try:
await self.database_client.read()
except Exception:
Expand All @@ -73,6 +78,7 @@ async def create_conversation(self, user_id, title=""):
"updatedAt": datetime.utcnow().isoformat(),
"userId": user_id,
"title": title,
"metadata": {}
}
# TODO: add some error handling based on the output of the upsert_item call
resp = await self.container_client.upsert_item(conversation)
Expand Down
Loading