The health_check() method in db.py currently uses a generic except Exception as a fallback after catching OperationalError. This can mask programming faults (e.g., AttributeError, TypeError) and make debugging harder. The fallback handler should catch only database‑layer exceptions that are expected when the database is unhealthy, specifically SQLAlchemyError.
Expected Behavior
Database connectivity issues should be reported as ok=False with appropriate reason.
Other errors (e.g., NameError, misused attribute) should not be caught silently; they should propagate to reveal bugs.
The fallback handler should only suppress SQLAlchemy‑specific exceptionsthat are notOperationalError` (though SQLAlchemyError covers them, we already catch OperationalError separately).
Actual Behavior
Any unexpected exception is caught and returns {"ok": False, "db_reachable": False, ...}, making it difficult to detect programming errors in the health check logic.
Proposed Fix
Add SQLAlchemyError to the import list from sqlalchemy.exc.
Replace except Exception: with except SQLAlchemyError:.
Updated code:
from sqlalchemy.exc import OperationalError, IntegrityError, SQLAlchemyError
# ... inside health_check ...
except SQLAlchemyError: # pragma: no cover
return {
"ok": False,
"db_reachable": False,
"reason": "database health query failed",
}
Reference
The
health_check()method indb.pycurrently uses a genericexcept Exceptionas a fallback after catchingOperationalError. This can mask programming faults (e.g., AttributeError, TypeError) and make debugging harder. The fallback handler should catch only database‑layer exceptions that are expected when the database is unhealthy, specificallySQLAlchemyError.Expected Behavior
Database connectivity issues should be reported as
ok=Falsewith appropriate reason.Other errors (e.g., NameError, misused attribute) should not be caught silently; they should propagate to reveal bugs.
The fallback handler should only suppress SQLAlchemy‑specific exceptions
that are notOperationalError` (though SQLAlchemyError covers them, we already catch OperationalError separately).Actual Behavior
Any unexpected exception is caught and returns
{"ok": False, "db_reachable": False, ...}, making it difficult to detect programming errors in the health check logic.Proposed Fix
Add
SQLAlchemyErrorto the import list fromsqlalchemy.exc.Replace
except Exception:withexcept SQLAlchemyError:.Updated code:
Reference