Ticket #16770: unwrap_templatesyntaxerror.diff

File unwrap_templatesyntaxerror.diff, 7.4 KB (added by jMyles, 13 years ago)

OK, this patch is feeling a little more final. I have some concerns about the fact that there seems to be no branch whatsoever that causes execution of the except block of line 79 of the template/debug. I'll get confirmation.

  • django/views/debug.py

     
    223223                    'loader': loader_name,
    224224                    'templates': template_list,
    225225                })
    226         if (settings.TEMPLATE_DEBUG and hasattr(self.exc_value, 'source') and
    227             isinstance(self.exc_value, TemplateSyntaxError)):
     226        if (settings.TEMPLATE_DEBUG and hasattr(self.exc_value, 'source_template_node')):
    228227            self.get_template_exception_info()
    229228
    230229        frames = self.get_traceback_frames()
     
    268267        return t.render(c)
    269268
    270269    def get_template_exception_info(self):
    271         origin, (start, end) = self.exc_value.source
     270        origin, (start, end) = self.exc_value.source_template_node
    272271        template_source = origin.reload()
    273272        context_lines = 10
    274273        line = 0
     
    626625{% endif %}
    627626{% if template_info %}
    628627<div id="template">
    629    <h2>Template error</h2>
     628   <h2>Error occured during template rendering</h2>
    630629   <p>In template <code>{{ template_info.name }}</code>, error at line <strong>{{ template_info.line }}</strong></p>
    631630   <h3>{{ template_info.message }}</h3>
    632631   <table class="source{% if template_info.top %} cut-top{% endif %}{% ifnotequal template_info.bottom template_info.total %} cut-bottom{% endifnotequal %}">
  • django/template/debug.py

     
    44from django.utils.safestring import SafeData, EscapeData
    55from django.utils.formats import localize
    66
     7
    78class DebugLexer(Lexer):
    89    def __init__(self, template_string, origin):
    910        super(DebugLexer, self).__init__(template_string, origin)
     
    4243    def error(self, token, msg):
    4344        return self.source_error(token.source, msg)
    4445
    45     def source_error(self, source,msg):
     46    def source_error(self, source, msg):
    4647        e = TemplateSyntaxError(msg)
    47         e.source = source
     48        e.source_template_node = source #Identify the template node that was being rendered when the error occurred.
    4849        return e
    4950
    5051    def create_nodelist(self):
     
    6768            e.source = token.source
    6869
    6970class DebugNodeList(NodeList):
     71    '''
     72    A list of nodes that is instantiated when debug is True - this is the nerve center of exceptions that occur during template rendering.
     73    '''
    7074    def render_node(self, node, context):
    7175        try:
    7276            result = node.render(context)
    7377        except TemplateSyntaxError, e:
    74             if not hasattr(e, 'source'):
    75                 e.source = node.source
    76             raise
     78            if not hasattr(e, 'source_template_node'): #Have we already identified the node where the problem occured?
     79                e.source_template_node = node.source #...if so, let's annotate the exception with that.
     80            raise       
    7781        except Exception, e:
    7882            from sys import exc_info
    79             wrapped = TemplateSyntaxError(u'Caught %s while rendering: %s' %
    80                 (e.__class__.__name__, force_unicode(e, errors='replace')))
    81             wrapped.source = getattr(e, 'template_node_source', node.source)
    82             wrapped.exc_info = exc_info()
    83             raise wrapped, None, wrapped.exc_info[2]
     83            e.source_template_node = getattr(e, 'template_node_source', node.source) #Again, annotating the exception with the node
     84            e.exc_info = exc_info()
     85            raise e, None, e.exc_info[2] #Make sure that ExceptionReporter gets the traceback
    8486        return result
    8587
    8688class DebugVariableNode(VariableNode):
  • django/views/debug.py

     
    223223                    'loader': loader_name,
    224224                    'templates': template_list,
    225225                })
    226         if (settings.TEMPLATE_DEBUG and hasattr(self.exc_value, 'source') and
    227             isinstance(self.exc_value, TemplateSyntaxError)):
     226        if (settings.TEMPLATE_DEBUG and hasattr(self.exc_value, 'source_template_node')):
    228227            self.get_template_exception_info()
    229228
    230229        frames = self.get_traceback_frames()
     
    268267        return t.render(c)
    269268
    270269    def get_template_exception_info(self):
    271         origin, (start, end) = self.exc_value.source
     270        origin, (start, end) = self.exc_value.source_template_node
    272271        template_source = origin.reload()
    273272        context_lines = 10
    274273        line = 0
     
    626625{% endif %}
    627626{% if template_info %}
    628627<div id="template">
    629    <h2>Template error</h2>
     628   <h2>Error occured during template rendering</h2>
    630629   <p>In template <code>{{ template_info.name }}</code>, error at line <strong>{{ template_info.line }}</strong></p>
    631630   <h3>{{ template_info.message }}</h3>
    632631   <table class="source{% if template_info.top %} cut-top{% endif %}{% ifnotequal template_info.bottom template_info.total %} cut-bottom{% endifnotequal %}">
  • django/template/debug.py

     
    44from django.utils.safestring import SafeData, EscapeData
    55from django.utils.formats import localize
    66
     7
    78class DebugLexer(Lexer):
    89    def __init__(self, template_string, origin):
    910        super(DebugLexer, self).__init__(template_string, origin)
     
    4243    def error(self, token, msg):
    4344        return self.source_error(token.source, msg)
    4445
    45     def source_error(self, source,msg):
     46    def source_error(self, source, msg):
    4647        e = TemplateSyntaxError(msg)
    47         e.source = source
     48        e.source_template_node = source #Identify the template node that was being rendered when the error occurred.
    4849        return e
    4950
    5051    def create_nodelist(self):
     
    6768            e.source = token.source
    6869
    6970class DebugNodeList(NodeList):
     71    '''
     72    A list of nodes that is instantiated when debug is True - this is the nerve center of exceptions that occur during template rendering.
     73    '''
    7074    def render_node(self, node, context):
    7175        try:
    7276            result = node.render(context)
    7377        except TemplateSyntaxError, e:
    74             if not hasattr(e, 'source'):
    75                 e.source = node.source
    76             raise
     78            if not hasattr(e, 'source_template_node'): #Have we already identified the node where the problem occured?
     79                e.source_template_node = node.source #...if so, let's annotate the exception with that.
     80            raise       
    7781        except Exception, e:
    7882            from sys import exc_info
    79             wrapped = TemplateSyntaxError(u'Caught %s while rendering: %s' %
    80                 (e.__class__.__name__, force_unicode(e, errors='replace')))
    81             wrapped.source = getattr(e, 'template_node_source', node.source)
    82             wrapped.exc_info = exc_info()
    83             raise wrapped, None, wrapped.exc_info[2]
     83            e.source_template_node = getattr(e, 'template_node_source', node.source) #Again, annotating the exception with the node
     84            e.exc_info = exc_info()
     85            raise e, None, e.exc_info[2] #Make sure that ExceptionReporter gets the traceback
    8486        return result
    8587
    8688class DebugVariableNode(VariableNode):
Back to Top