Skip to content

Add exception handling for DB transactions#228

Open
harsh-kumar-patwa wants to merge 2 commits intoThe-OpenROAD-Project:masterfrom
harsh-kumar-patwa:topic/db-exception-handling
Open

Add exception handling for DB transactions#228
harsh-kumar-patwa wants to merge 2 commits intoThe-OpenROAD-Project:masterfrom
harsh-kumar-patwa:topic/db-exception-handling

Conversation

@harsh-kumar-patwa
Copy link

Summary

Right now, if a database commit or delete fails in any of the CRUD functions, there's no rollback — the SQLAlchemy session is left in a broken state. The API endpoints also let these errors bubble up as raw 500s, and the chat endpoints crash instead of falling back to in-memory history.

This PR fixes that.

Changes

  • crud.py — wrapped all 5 write functions (create_conversation, update_conversation_title, delete_conversation, create_message, delete_message) with try/except so they call db.rollback() on failure before re-raising
  • conversations.py — CRUD endpoints now catch DB errors and return proper HTTP 500 responses. Chat endpoints use a db_persist flag so if the DB call fails, they fall back to in-memory history instead of crashing. get_history_str also handles DB read failures gracefully.
  • test_database_crud.py — added 5 tests to verify rollback is called when a commit fails

Context

Resolves #185

All existing tests pass (54/54), no lint issues.

Copilot AI review requested due to automatic review settings March 1, 2026 11:56
@harsh-kumar-patwa harsh-kumar-patwa force-pushed the topic/db-exception-handling branch from 1257bb6 to 342da80 Compare March 1, 2026 11:58
Copy link
Contributor

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 improves resilience around SQLAlchemy session failures by ensuring failed write operations roll back transactions and by adding API-layer handling for database exceptions, including a fallback for chat history persistence.

Changes:

  • Add try/except + db.rollback() around DB write operations in crud.py (and log before re-raising).
  • Catch SQLAlchemyError in conversation CRUD endpoints and convert them into explicit HTTP 500 responses.
  • Add tests asserting rollback() is invoked when commit() fails in each write CRUD function.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
backend/src/database/crud.py Wraps write operations with rollback + logging on SQLAlchemyError.
backend/src/api/routers/conversations.py Adds DB exception handling for CRUD endpoints and introduces db_persist fallback logic for chat persistence.
backend/tests/test_database_crud.py Adds unit tests to verify rollback behavior on commit failures.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Signed-off-by: Harsh Kumar <harshkumar3446@gmail.com>
@harsh-kumar-patwa harsh-kumar-patwa force-pushed the topic/db-exception-handling branch from 342da80 to cac4c4d Compare March 1, 2026 12:06
Signed-off-by: Harsh Kumar <harshkumar3446@gmail.com>
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.

add exception handling for db transactions

2 participants