Skip to content

Commit

Permalink
fix: recursive error while rendering flexmeasures_template (#1302)
Browse files Browse the repository at this point in the history
* fix: recursive error while rendering flexmeasures_template

Signed-off-by: F.N. Claessen <[email protected]>

* fix: arg in kwargs

Signed-off-by: F.N. Claessen <[email protected]>

* docs: changelog entry

Signed-off-by: F.N. Claessen <[email protected]>

* Revert "fix: arg in kwargs"

This reverts commit d3530c2.

* Revert "fix: recursive error while rendering flexmeasures_template"

This reverts commit ab8fe03.

* fix: catch any error while rendering the FlexMeasures template, and fall back to using the default Flask template

Signed-off-by: F.N. Claessen <[email protected]>

* feat: log error showing why the flexmeasures_template failed to load

Signed-off-by: F.N. Claessen <[email protected]>

* feat: log original error, too

Signed-off-by: F.N. Claessen <[email protected]>

* Revert "feat: log original error, too" (already logged previously)

This reverts commit 49e8003.

* fix: warning instead of error, also more informative message

Signed-off-by: F.N. Claessen <[email protected]>

---------

Signed-off-by: F.N. Claessen <[email protected]>
Co-authored-by: Nicolas Höning <[email protected]>
  • Loading branch information
Flix6x and nhoening authored Jan 3, 2025
1 parent d3bfe9c commit acd3f62
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions documentation/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Infrastructure / Support
* For MacOS developers, install HiGHS solver automatically [see `PR #1187 <https://github.com/FlexMeasures/flexmeasures/pull/1187>`_]
* Migrate data for the ``sensors_to_show`` asset attribute to a dedicated column in the database table for assets [see `PR #1200 <https://github.com/FlexMeasures/flexmeasures/pull/1200>`_ and `PR #1282 <https://github.com/FlexMeasures/flexmeasures/pull/1282>`_]
* Add support for installing FlexMeasures under Python 3.12 [see `PR #1233 <https://github.com/FlexMeasures/flexmeasures/pull/1233>`_]
* Better error handling in UI, for example, in case of a forgotten ``flexmeasures db upgrade`` [see `PR #1302 <https://github.com/FlexMeasures/flexmeasures/pull/1302>`_]

Bugfixes
-----------
Expand Down
19 changes: 19 additions & 0 deletions flexmeasures/ui/utils/view_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

from functools import wraps
import json
import os
import subprocess
Expand All @@ -22,6 +23,24 @@
from flexmeasures.ui.utils.color_defaults import get_color_settings


def fall_back_to_flask_template(render_function):
"""In case the render_function is raising an error, fall back to using flask.render_template."""

@wraps(render_function)
def wrapper(template_name, *args, **kwargs):
try:
return render_function(template_name, *args, **kwargs)
except Exception as e:
current_app.logger.warning(
f"""Rendering via Flask's render_template("{template_name}"). """
f"""Failed to render via {render_function.__name__}("{template_name}") due to {e}."""
)
return render_template(template_name, **kwargs)

return wrapper


@fall_back_to_flask_template
def render_flexmeasures_template(html_filename: str, **variables):
"""Render template and add all expected template variables, plus the ones given as **variables."""
variables["FLEXMEASURES_ENFORCE_SECURE_CONTENT_POLICY"] = current_app.config.get(
Expand Down

0 comments on commit acd3f62

Please sign in to comment.