Skip to content

Conversation

NirajC-Microsoft
Copy link
Owner

@NirajC-Microsoft NirajC-Microsoft commented May 14, 2025

Purpose

This pull request includes changes to the CosmosDBService class in src/backend/history/cosmosdbservice.py, focusing on error handling improvements and adding metadata initialization for new conversations. The most important changes are grouped below:

Error Handling Updates:

  • Updated the exception message for unauthorized access from "Invalid credentials" to "Unauthorized" for better clarity.
  • Replaced a specific error message for a CosmosDB endpoint issue with a generic "Something went wrong!" message and added a print statement for debugging purposes.
  • Added general exception handling (except:) for database and container client initialization to prevent unhandled exceptions.
  • Modified the error message in the ensure method from "CosmosDB client not initialized correctly" to "client not initialized" for brevity.

Metadata Initialization:

  • Added a metadata field initialized as an empty dictionary when creating a new conversation in the create_conversation method.

Does this introduce a breaking change?

  • Yes
  • No

Golden Path Validation

  • I have tested the primary workflows (the "golden path") to ensure they function correctly without errors.

Deployment Validation

  • I have validated the deployment process successfully and all services are running as expected with this change.

What to Check

Verify that the following are valid

  • I have built and tested the code locally and in a deployed app
  • For frontend changes, I have pulled the latest code from main, built the frontend, and committed all static files.
  • This is a change for all users of this app. No code or asset is specific to my use case or my organization.

copilot:walkthough

copilot:poem

Other Information

@NirajC-Microsoft NirajC-Microsoft requested a review from Copilot May 14, 2025 04:08
Copilot

This comment was marked as resolved.

@NirajC-Microsoft NirajC-Microsoft requested a review from Copilot May 14, 2025 04:18
Copilot

This comment was marked as outdated.

@NirajC-Microsoft NirajC-Microsoft requested a review from Copilot May 14, 2025 06:12
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates error handling in the CosmosDB service initialization and the ensure method, and adds a default metadata field when creating a conversation.

  • Refines exception messages for unauthorized access and general errors in the client constructor.
  • Introduces bare except blocks (suppressing unexpected errors) for database and container client setup.
  • Updates ensure() return message and adds a "metadata": {} field in create_conversation().

Comment on lines 30 to 31
print("Cosmos error")
raise ValueError("Something went wrong!") from e
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 generic error message 'Something went wrong!' lacks context for debugging; consider using a more informative message or including details from the caught exception.

Suggested change
print("Cosmos error")
raise ValueError("Something went wrong!") from e
error_message = f"Cosmos error: {e.message} (Status code: {e.status_code})"
raise ValueError(error_message) from e

Copilot uses AI. Check for mistakes.

Comment on lines 39 to 40
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.

Comment on lines +48 to +49
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 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 52 to +57
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"
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.

committing copilot suggestion

Co-authored-by: Copilot <[email protected]>
@NirajC-Microsoft NirajC-Microsoft requested a review from Copilot May 16, 2025 05:02
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR focuses on improving error handling in the CosmosDBService class and adds metadata initialization for new conversation creation.

  • Updated exception messages for unauthorized access and CosmosDB endpoint issues.
  • Introduced catch-all exception blocks for database and container client initialization.
  • Added a metadata field in the conversation creation workflow.

Comment on lines +48 to +49
except:
pass
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.

@NirajC-Microsoft NirajC-Microsoft requested a review from Copilot May 16, 2025 05:05
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request refines error handling in the CosmosDBService class while also adding metadata initialization for new conversations. Key changes include:

  • Updating exception messages for unauthorized access and CosmosDB endpoint issues.
  • Introducing general exception handling with logging for database and container client initialization.
  • Adding a metadata field (with duplicate comments) during conversation creation.

Comment on lines +49 to +50
except:
pass
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.

@@ -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.
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.

[nitpick] There is a duplicate comment block for the metadata field. Removing the redundant comment would improve clarity and reduce clutter.

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant