diff --git a/documentation/changelog.rst b/documentation/changelog.rst index 553b87a653..15369977cd 100644 --- a/documentation/changelog.rst +++ b/documentation/changelog.rst @@ -38,6 +38,7 @@ Infrastructure / Support Bugfixes ----------- +* Fix error page rendering when a database error leaves the transaction in a failed state (e.g. when a missing migration such as the one adding ``account.attributes`` causes an ``InFailedSqlTransaction`` error) [see `PR #2100 `_] v0.31.3 | April 11, 2026 diff --git a/flexmeasures/utils/error_utils.py b/flexmeasures/utils/error_utils.py index 1da731530d..d768172591 100644 --- a/flexmeasures/utils/error_utils.py +++ b/flexmeasures/utils/error_utils.py @@ -89,6 +89,22 @@ def error_handling_router(error: HTTPException): else: log_error(error, getattr(error, "description", str(error))) + # Roll back any failed DB transaction so subsequent queries (e.g. in + # the error-page template) can run in a clean state. This is needed + # when a database error (e.g. UndefinedColumn due to a missing + # migration) leaves the PostgreSQL transaction in an aborted state; + # without the rollback every following query in the same request would + # fail with "InFailedSqlTransaction" and the error page itself could + # not be rendered properly. + try: + from flexmeasures.data import db + + db.session.rollback() + except Exception as rollback_exc: + current_app.logger.warning( + "Could not roll back DB session in error handler: %s", rollback_exc + ) + error_text = getattr( error, "description", f"Something went wrong: {error.__class__.__name__}" )