Skip to content

Commit

Permalink
Merge pull request #7 from WnP/handling_exceptions
Browse files Browse the repository at this point in the history
Handling exceptions during rendering
  • Loading branch information
iamjazzar authored Jan 29, 2018
2 parents a22f312 + 73e98c2 commit ae49c80
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion djangomako/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from django.utils.module_loading import import_string

from mako.template import Template as MakoTemplate
from mako.exceptions import RichTraceback
from mako import exceptions as mako_exceptions


Expand Down Expand Up @@ -164,7 +165,38 @@ def render(self, context=None, request=None):
context['csrf_input'] = csrf_input_lazy(request)
context['csrf_token'] = csrf_token_lazy(request)

return self.template.render(**context)
try:
return self.template.render(**context)
except Exception as e:
traceback = RichTraceback()

source = traceback.source
if not source:
# There's no template source lines then raise
raise e

source = source.split('\n')
line = traceback.lineno
top = max(0, line - 4)
bottom = min(len(source), line + 5)
source_lines = [(i + 1, source[i]) for i in range(top, bottom)]

e.template_debug = {
'name': traceback.records[5][4],
'message': '{}: {}'.format(
traceback.errorname, traceback.message),
'source_lines': source_lines,
'line': line,
'during': source_lines[line - top - 1][1],
'total': bottom - top,
'bottom': bottom,
'top': top + 1,
# mako's RichTraceback doesn't return column number
'before': '',
'after': '',
}

raise e

@staticmethod
def get_reverse_url():
Expand Down

0 comments on commit ae49c80

Please sign in to comment.