Skip to content

[Fix] #104 — Add error handling to startup event for init_db() - #127

Open
adarsh-gautam-sys wants to merge 1 commit into
devloperdevesh:mainfrom
adarsh-gautam-sys:fix/issue-104-startup-error-handling
Open

[Fix] #104 — Add error handling to startup event for init_db()#127
adarsh-gautam-sys wants to merge 1 commit into
devloperdevesh:mainfrom
adarsh-gautam-sys:fix/issue-104-startup-error-handling

Conversation

@adarsh-gautam-sys

Copy link
Copy Markdown
Contributor

Summary

The startup_event in app/main.py called init_db() with no error handling. If the database was unreachable or misconfigured, the application would silently start in a broken state, causing cryptic runtime errors on every API call instead of a clear startup failure. This PR wraps init_db() in a try/except block that logs the error and aborts startup with a RuntimeError.

Root Cause

app/main.py startup event called init_db() without try/except. Any exception during database initialisation was silently swallowed (or propagated non-fatally by FastAPI), allowing the application to start and appear healthy while the database remained uninitialized.

Changes Made

  • app/main.py:
    • Added import logging and a module-level logger
    • Wrapped init_db() call in try/except Exception; on failure, logs a CRITICAL message and raises RuntimeError to abort startup
    • Also wrapped init_redis() in a separate try/except; Redis failure is logged as WARNING (non-fatal, degrades gracefully)
    • Fixed duplicate imports and consolidated into clean, single-app structure
    • Fixed middleware imports to use correct app.core.* paths

How to Test

  1. Configure DATABASE_URL to point to an unreachable database.
  2. Start the application: uvicorn app.main:app --reload
  3. Observe the process exits immediately with RuntimeError: Startup aborted — database unavailable and a CRITICAL log line.
  4. With a valid database, confirm startup proceeds normally with INFO log lines.

Related Issue

Closes #104

@vercel

vercel Bot commented Jun 21, 2026

Copy link
Copy Markdown

@adarsh-gautam-sys is attempting to deploy a commit to the devloperdevesh's projects Team on Vercel.

A member of the Team first needs to authorize it.

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.

No error handling in startup event, missing database initialization silently fails

1 participant