From 3567506c2d5f787fe14faf9e77dc2a6d92723152 Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Thu, 23 May 2013 16:24:16 -0600 Subject: [PATCH 1/2] Add line number to Jinja errors, Fix #4966 Thanks to @Mrten to figuring this one out. =) --- salt/utils/templates.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/salt/utils/templates.py b/salt/utils/templates.py index 4936e71b8c..1316bfc9d5 100644 --- a/salt/utils/templates.py +++ b/salt/utils/templates.py @@ -11,6 +11,7 @@ import imp import logging import tempfile import traceback +import sys # Import third party libs import jinja2 @@ -105,7 +106,7 @@ def render_jinja_tmpl(tmplstr, context, tmplpath=None): try: output = jinja_env.from_string(tmplstr).render(**context) except jinja2.exceptions.TemplateSyntaxError as exc: - raise SaltTemplateRenderError(str(exc)) + raise SaltTemplateRenderError(str(exc) + '; lineno: ' + str(traceback.extract_tb(sys.exc_info()[2])[-1][1])) # Workaround a bug in Jinja that removes the final newline # (https://github.com/mitsuhiko/jinja2/issues/75) From 695dc745d43332efde086970c2593e92c95217c7 Mon Sep 17 00:00:00 2001 From: Colton Myers Date: Thu, 23 May 2013 16:33:21 -0600 Subject: [PATCH 2/2] Clean up the error message --- salt/utils/templates.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/salt/utils/templates.py b/salt/utils/templates.py index 1316bfc9d5..f25b7edcfc 100644 --- a/salt/utils/templates.py +++ b/salt/utils/templates.py @@ -102,11 +102,15 @@ def render_jinja_tmpl(tmplstr, context, tmplpath=None): jinja_env = jinja2.Environment(**env_args) else: jinja_env = jinja2.Environment( - undefined=jinja2.StrictUndefined,**env_args) + undefined=jinja2.StrictUndefined, **env_args) try: output = jinja_env.from_string(tmplstr).render(**context) except jinja2.exceptions.TemplateSyntaxError as exc: - raise SaltTemplateRenderError(str(exc) + '; lineno: ' + str(traceback.extract_tb(sys.exc_info()[2])[-1][1])) + error = '{0}; line {1} in template'.format( + exc, + traceback.extract_tb(sys.exc_info()[2])[-1][1] + ) + raise SaltTemplateRenderError(error) # Workaround a bug in Jinja that removes the final newline # (https://github.com/mitsuhiko/jinja2/issues/75)