Catch exceptions when error rendering templates
This commit is contained in:
parent
5c63862054
commit
b1bf6a609e
3 changed files with 28 additions and 5 deletions
|
@ -14,3 +14,10 @@ class InvalidEntityFormatError(HomeAssistantError):
|
||||||
class NoEntitySpecifiedError(HomeAssistantError):
|
class NoEntitySpecifiedError(HomeAssistantError):
|
||||||
""" When no entity is specified. """
|
""" When no entity is specified. """
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class TemplateError(HomeAssistantError):
|
||||||
|
""" Error during template rendering. """
|
||||||
|
def __init__(self, exception):
|
||||||
|
super().__init__('{}: {}'.format(exception.__class__.__name__,
|
||||||
|
exception))
|
||||||
|
|
|
@ -6,7 +6,12 @@ Template utility methods for rendering strings with HA data.
|
||||||
"""
|
"""
|
||||||
# pylint: disable=too-few-public-methods
|
# pylint: disable=too-few-public-methods
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
|
import jinja2
|
||||||
from jinja2.sandbox import ImmutableSandboxedEnvironment
|
from jinja2.sandbox import ImmutableSandboxedEnvironment
|
||||||
|
from homeassistant.exceptions import TemplateError
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def render_with_possible_json_value(hass, template, value):
|
def render_with_possible_json_value(hass, template, value):
|
||||||
|
@ -20,7 +25,11 @@ def render_with_possible_json_value(hass, template, value):
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return render(hass, template, variables)
|
try:
|
||||||
|
return render(hass, template, variables)
|
||||||
|
except TemplateError:
|
||||||
|
_LOGGER.exception('Error parsing value')
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
def render(hass, template, variables=None, **kwargs):
|
def render(hass, template, variables=None, **kwargs):
|
||||||
|
@ -28,9 +37,12 @@ def render(hass, template, variables=None, **kwargs):
|
||||||
if variables is not None:
|
if variables is not None:
|
||||||
kwargs.update(variables)
|
kwargs.update(variables)
|
||||||
|
|
||||||
return ENV.from_string(template, {
|
try:
|
||||||
'states': AllStates(hass)
|
return ENV.from_string(template, {
|
||||||
}).render(kwargs)
|
'states': AllStates(hass)
|
||||||
|
}).render(kwargs)
|
||||||
|
except jinja2.TemplateError as err:
|
||||||
|
raise TemplateError(err)
|
||||||
|
|
||||||
|
|
||||||
class AllStates(object):
|
class AllStates(object):
|
||||||
|
|
|
@ -7,7 +7,7 @@ Tests Home Assistant util methods.
|
||||||
# pylint: disable=too-many-public-methods
|
# pylint: disable=too-many-public-methods
|
||||||
import unittest
|
import unittest
|
||||||
import homeassistant.core as ha
|
import homeassistant.core as ha
|
||||||
|
from homeassistant.exceptions import TemplateError
|
||||||
from homeassistant.util import template
|
from homeassistant.util import template
|
||||||
|
|
||||||
|
|
||||||
|
@ -84,3 +84,7 @@ class TestUtilTemplate(unittest.TestCase):
|
||||||
'',
|
'',
|
||||||
template.render_with_possible_json_value(
|
template.render_with_possible_json_value(
|
||||||
self.hass, '{{ value_json }}', '{ I AM NOT JSON }'))
|
self.hass, '{{ value_json }}', '{ I AM NOT JSON }'))
|
||||||
|
|
||||||
|
def test_raise_exception_on_error(self):
|
||||||
|
with self.assertRaises(TemplateError):
|
||||||
|
template.render(self.hass, '{{ invalid_syntax')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue