Skip to content

fix(ai): handle db failures in session history fetch (#83)#125

Open
qscetxxxl wants to merge 1 commit into
rishabh0510rishabh:mainfrom
qscetxxxl:fix/fetch-history-exceptions
Open

fix(ai): handle db failures in session history fetch (#83)#125
qscetxxxl wants to merge 1 commit into
rishabh0510rishabh:mainfrom
qscetxxxl:fix/fetch-history-exceptions

Conversation

@qscetxxxl
Copy link
Copy Markdown

@qscetxxxl qscetxxxl commented May 22, 2026

Description

Refactored _fetch_session_history in backend/app/ai/service.py to stop using broad exception blocks.

  • Catches OperationalError to raise a 500 Internal Server Error if the PostgreSQL database is entirely unreachable.
  • Catches generic SQLAlchemyError for transient query issues to log them and safely return an empty list.

Fixes #83

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling for database connectivity issues, distinguishing between critical failures and transient errors to provide clearer feedback to users when the service is temporarily unavailable.

Review Change Stack

@vercel
Copy link
Copy Markdown

vercel Bot commented May 22, 2026

@qscetxxxl is attempting to deploy a commit to the rishabhmishra0510-5147's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 22, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1afa18af-9ffb-4d47-b7ae-b3fa0127bfc2

📥 Commits

Reviewing files that changed from the base of the PR and between fca82d4 and 98cb5bb.

📒 Files selected for processing (1)
  • backend/app/ai/service.py

📝 Walkthrough

Walkthrough

The PR refines error handling in the AI service's session history retrieval method. Previously, a broad exception catch masked database failures by silently returning empty history. The changes now distinguish critical database connectivity loss (OperationalError) from transient errors, raising an HTTP 500 for connectivity failures while logging and gracefully degrading for transient issues.

Changes

AI Service Database Error Handling

Layer / File(s) Summary
Exception handling for session history fetch
backend/app/ai/service.py
Imports HTTPException, status, and OperationalError, then refactors _fetch_session_history to raise HTTP 500 on critical database connectivity loss while logging and suppressing transient SQLAlchemyError cases.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A rabbit hops through database logs,
Spotting errors hiding in the fog—
Now when the connection falls away,
The service speaks up right away! 🌐

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately summarizes the main change: handling database failures in session history fetch, matching the code modifications.
Linked Issues check ✅ Passed The code changes implement all objectives from issue #83: distinguish OperationalError (500 response) from transient SQLAlchemyError (logged, empty list returned), preventing silent failures.
Out of Scope Changes check ✅ Passed All changes in backend/app/ai/service.py are directly aligned with issue #83 requirements for refactoring database error handling; no out-of-scope modifications detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@rishabh0510rishabh
Copy link
Copy Markdown
Owner

hey @qscetxxxl
Thank you for your contribution!
The CI workflow is currently failing due to a linting error: there is a trailing whitespace on line 210 of backend/app/ai/service.py.
Please remove any extra spaces at the end of that line and push the update. Once fixed, the checks should pass.

Let me know if you need any assistance!

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.

[Bug] Broad exception handling in _fetch_session_history masks database failures

2 participants