Ensure all jinja2 errors are trapped and displayed in the developer tools (#40624)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
J. Nick Koston 2020-09-26 17:03:32 -05:00 committed by GitHub
parent 3261a904da
commit 57b7559832
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 89 additions and 22 deletions

View file

@ -17,6 +17,7 @@ from homeassistant.exceptions import (
from homeassistant.helpers import config_validation as cv, entity
from homeassistant.helpers.event import TrackTemplate, async_track_template_result
from homeassistant.helpers.service import async_get_all_descriptions
from homeassistant.helpers.template import Template
from homeassistant.loader import IntegrationNotFound, async_get_integration
from . import const, decorators, messages
@ -242,16 +243,15 @@ def handle_ping(hass, connection, msg):
@decorators.websocket_command(
{
vol.Required("type"): "render_template",
vol.Required("template"): cv.template,
vol.Required("template"): str,
vol.Optional("entity_ids"): cv.entity_ids,
vol.Optional("variables"): dict,
}
)
def handle_render_template(hass, connection, msg):
"""Handle render_template command."""
template = msg["template"]
template.hass = hass
template_str = msg["template"]
template = Template(template_str, hass)
variables = msg.get("variables")
info = None
@ -261,13 +261,8 @@ def handle_render_template(hass, connection, msg):
track_template_result = updates.pop()
result = track_template_result.result
if isinstance(result, TemplateError):
_LOGGER.error(
"TemplateError('%s') " "while processing template '%s'",
result,
track_template_result.template,
)
result = None
connection.send_error(msg["id"], const.ERR_TEMPLATE_ERROR, str(result))
return
connection.send_message(
messages.event_message(
@ -275,9 +270,16 @@ def handle_render_template(hass, connection, msg):
)
)
info = async_track_template_result(
hass, [TrackTemplate(template, variables)], _template_listener
)
try:
info = async_track_template_result(
hass,
[TrackTemplate(template, variables)],
_template_listener,
raise_on_template_error=True,
)
except TemplateError as ex:
connection.send_error(msg["id"], const.ERR_TEMPLATE_ERROR, str(ex))
return
connection.subscriptions[msg["id"]] = info.async_remove