Add try/catch handler for Jinja exceptions so we can better annotate

errors, to include the file name.
pull/102/head
Victor Agababov 9 years ago
parent 8a7fcb24d9
commit 29009026e6

@ -231,7 +231,8 @@ def ExpandTemplate(resource, imports, env, validate_schema=False):
raise ExpansionError(resource, e.message) raise ExpansionError(resource, e.message)
if source_file.endswith('jinja'): if source_file.endswith('jinja'):
expanded_template = ExpandJinja(imports[source_file], resource, imports) expanded_template = ExpandJinja(
source_file, imports[source_file], resource, imports)
elif source_file.endswith('py'): elif source_file.endswith('py'):
# This is a Python template. # This is a Python template.
expanded_template = ExpandPython( expanded_template = ExpandPython(
@ -252,10 +253,11 @@ def ExpandTemplate(resource, imports, env, validate_schema=False):
return parsed_template return parsed_template
def ExpandJinja(source_template, resource, imports): def ExpandJinja(file_name, source_template, resource, imports):
"""Render the jinja template using jinja libraries. """Render the jinja template using jinja libraries.
Args: Args:
file_name: string, the file name.
source_template: string, the content of jinja file to be render source_template: string, the content of jinja file to be render
resource: resource object, the resource that contains parameters to the resource: resource object, the resource that contains parameters to the
jinja file jinja file
@ -263,10 +265,11 @@ def ExpandJinja(source_template, resource, imports):
and contents and contents
Returns: Returns:
The final expanded template The final expanded template
Raises:
ExpansionError in case we fail to expand the Jinja2 template.
""" """
# The standard jinja loader doesn't work in the sandbox as it calls getmtime() try:
# and this system call is not supported.
env = jinja2.Environment(loader=jinja2.DictLoader(imports)) env = jinja2.Environment(loader=jinja2.DictLoader(imports))
template = env.from_string(source_template) template = env.from_string(source_template)
@ -276,6 +279,9 @@ def ExpandJinja(source_template, resource, imports):
return template.render(resource) return template.render(resource)
else: else:
return template.render() return template.render()
except Exception:
st = 'Exception in %s\n%s'%(file_name, traceback.format_exc())
raise ExpansionError(file_name, st)
def ExpandPython(python_source, file_name, params): def ExpandPython(python_source, file_name, params):

Loading…
Cancel
Save