def extract_jinja_error(exc_info, fnames_prefix=None):
'''
Extract relevant informations from a Jinja2 exception.
*exc_info* should be a ``(exc_type, exc_value, traceback)`` tuple, as
returned by :func:`sys.exc_info`.
Return a ``(stack, error)`` tuple, where *stack* a list of lines describing
the stack that led to the error, and *error* the description of the error.
Raise a :class:`TypeError` if the error is not a supported Jinja2 error.
'''
exc_type, exc_value, tb = exc_info
if exc_type is jinja2.UndefinedError:
prefix = u'Undefined variable'
elif exc_type is jinja2.TemplateSyntaxError:
prefix = u'Syntax error'
else:
raise TypeError(exc_type)
stack_lines = []
jinja_tb = [x for x in traceback.extract_tb(tb)
if x[2] in ('top-level template code', 'template')]
for file_name, line, func_name, text in jinja_tb:
if fnames_prefix is not None:
file_name = file_name[len(fnames_prefix) + 1:]
stack_lines.append(u' File "%s", line %s' % (file_name, line))
stack_lines.append(u' %s' % text)
error_details = unicode(exc_value)
error_details = error_details.rstrip('.')
return stack_lines, u'%s: %s' % (prefix, error_details)
评论列表
文章目录