Skip to content

Fix overly broad exception handling in Node_collection.health_check to catch SQLAlchemyError only #940

Description

@Bornunique911

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions