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:
1 parent
3261a904da
commit
57b7559832
6 files changed
+89
-22
No files matched your search
@@ -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
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ ERR_UNKNOWN_COMMAND = "unknown_command"
|
||||
ERR_UNKNOWN_ERROR = "unknown_error"
|
||||
ERR_UNAUTHORIZED = "unauthorized"
|
||||
ERR_TIMEOUT = "timeout"
|
||||
ERR_TEMPLATE_ERROR = "template_error"
|
||||
|
||||
TYPE_RESULT = "result"
|
||||
|
||||
|
||||
Reference in new issue
Block a user