From 03eebc4f9b4afc2e180dcf53275ce03301e39bba Mon Sep 17 00:00:00 2001 From: Martin Hohenberg Date: Thu, 18 Jun 2026 21:10:38 +0200 Subject: [PATCH] Expose DB connection error in /health response for debugging Co-Authored-By: Claude Sonnet 4.6 --- app/db.py | 8 ++++---- app/main.py | 12 +++++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/app/db.py b/app/db.py index dd4a356..9a2c403 100644 --- a/app/db.py +++ b/app/db.py @@ -13,10 +13,10 @@ def _params() -> dict: } -def check_db() -> bool: +def check_db() -> tuple[bool, str]: try: conn = pymysql.connect(**_params()) conn.close() - return True - except Exception: - return False + return True, "ok" + except Exception as e: + return False, str(e) diff --git a/app/main.py b/app/main.py index e516e6f..d24e6e2 100644 --- a/app/main.py +++ b/app/main.py @@ -10,10 +10,11 @@ logger = logging.getLogger("pops") @asynccontextmanager async def lifespan(app: FastAPI): - if check_db(): + ok, msg = check_db() + if ok: logger.info("Database connection OK") else: - logger.warning("Database unreachable — check MYSQL_* environment variables") + logger.warning("Database unreachable: %s", msg) yield @@ -27,8 +28,9 @@ def root(): @app.get("/health") def health(): - db_ok = check_db() + ok, msg = check_db() return { - "status": "ok" if db_ok else "degraded", - "db": "connected" if db_ok else "unreachable", + "status": "ok" if ok else "degraded", + "db": "connected" if ok else "unreachable", + "detail": msg, }