From 73e98c2b91bdbe7641f569f1818b44fb5b4b334a Mon Sep 17 00:00:00 2001 From: Steeve Chailloux Date: Sat, 6 Jan 2018 18:58:33 +0100 Subject: [PATCH] Handling exceptions during rendering --- djangomako/backends.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/djangomako/backends.py b/djangomako/backends.py index a53d33e..3179c97 100644 --- a/djangomako/backends.py +++ b/djangomako/backends.py @@ -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 @@ -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():