Skip to content

Utils

safe_commit(session, *, error_msg)

Commits the session, making sure it is rolled back in case the commit fails.

Parameters:

Name Type Description Default
error_msg str

The message for the raised exception.

required

Raises:

Type Description
CommitFailed

If committing the session failed.

Source code in sqlmodelservice/utils.py
def safe_commit(session: "Session", *, error_msg: str) -> None:
    """
    Commits the session, making sure it is rolled back in case the commit fails.

    Arguments:
        error_msg: The message for the raised exception.

    Raises:
        CommitFailed: If committing the session failed.
    """
    try:
        session.commit()
    except Exception as e:
        session.rollback()
        raise CommitFailed(error_msg) from e