def render_node(self, node, context): try: result = node.render(context) except TemplateSyntaxError as e: if not hasattr(e, 'source'): e.source = node.source raise except Exception as e: from sys import exc_info wrapped = TemplateSyntaxError('Caught an exception while rendering: %s' % force_unicode(e, errors='replace')) wrapped.source = node.source wrapped.exc_info = exc_info() raise wrapped return result
def render_node(self, node, context): try: result = node.render(context) except TemplateSyntaxError as e: if not hasattr(e, 'source'): e.source = node.source raise except Exception as e: from sys import exc_info wrapped = TemplateSyntaxError( 'Caught an exception while rendering: %s' % force_unicode(e, errors='replace')) wrapped.source = node.source wrapped.exc_info = exc_info() raise wrapped return result
def source_error(self, source, msg): e = TemplateSyntaxError(msg) e.source = source return e
def source_error(self, source,msg): e = TemplateSyntaxError(msg) e.source = source return e
def _generate_django_exception(e, source=None): '''Generate a Django exception from a Jinja exception''' from django.views.debug import linebreak_iter import re if source: exception = DjangoTemplateSyntaxError(e.message) exception_dict = e.__dict__ del exception_dict['source'] # Fetch the entire template in a string template_string = source[0].reload() # Get the line number from the error message, if available match = re.match('.* at (\d+)$', e.message) start_index = 0 stop_index = 0 if match: # Convert the position found in the stacktrace to a position # the Django template debug system can use position = int(match.group(1)) + source[1][0] + 1 for index in linebreak_iter(template_string): if index >= position: stop_index = min(index, position + 3) start_index = min(index, position - 2) break start_index = index else: # So there wasn't a matching error message, in that case we # simply have to highlight the entire line instead of the specific # words ignore_lines = -1 for i, index in enumerate(linebreak_iter(template_string)): if source[1][0] > index: ignore_lines += 1 if i - ignore_lines == e.lineno: stop_index = index break start_index = index # Convert the positions to a source that is compatible with the # Django template debugger source = source[0], ( start_index, stop_index, ) else: # No source available so we let Django fetch it for us lineno = e.lineno - 1 template_string, source = django_loader.find_template_source(e.name) exception = DjangoTemplateSyntaxError(e.message) # Find the positions by the line number given in the exception start_index = 0 for i in range(lineno): start_index = template_string.index('\n', start_index + 1) source = source, ( start_index + 1, template_string.index('\n', start_index + 1) + 1, ) # Set our custom source as source for the exception so the Django # template debugger can use it exception.source = source return exception