mirror of
https://github.com/django/django.git
synced 2025-06-01 09:39:12 +00:00
Fixed #28050 -- Added template name to TemplateSyntaxError.
This commit is contained in:
parent
098c8bc99c
commit
4a293eff6f
@ -199,6 +199,14 @@ class Template:
|
||||
except Exception as e:
|
||||
if self.engine.debug:
|
||||
e.template_debug = self.get_exception_info(e, e.token)
|
||||
if (
|
||||
isinstance(e, TemplateSyntaxError)
|
||||
and self.origin.name != UNKNOWN_SOURCE
|
||||
and e.args
|
||||
):
|
||||
raw_message = e.args[0]
|
||||
e.raw_error_message = raw_message
|
||||
e.args = (f"Template: {self.origin.name}, {raw_message}", *e.args[1:])
|
||||
raise
|
||||
|
||||
def get_exception_info(self, exception, token):
|
||||
|
@ -414,7 +414,9 @@ class ExceptionReporter:
|
||||
if self.exc_type:
|
||||
c["exception_type"] = self.exc_type.__name__
|
||||
if self.exc_value:
|
||||
c["exception_value"] = str(self.exc_value)
|
||||
c["exception_value"] = getattr(
|
||||
self.exc_value, "raw_error_message", self.exc_value
|
||||
)
|
||||
if exc_notes := getattr(self.exc_value, "__notes__", None):
|
||||
c["exception_notes"] = "\n" + "\n".join(exc_notes)
|
||||
if frames:
|
||||
|
@ -1,7 +1,8 @@
|
||||
from django.template.base import TemplateSyntaxError
|
||||
from django.template.base import Origin, Template, TemplateSyntaxError
|
||||
from django.template.context import Context
|
||||
from django.template.loader_tags import BlockContext, BlockNode
|
||||
from django.test import SimpleTestCase
|
||||
from django.views.debug import ExceptionReporter
|
||||
|
||||
from ..utils import SilentAttrClass, SilentGetItemClass, SomeClass, setup
|
||||
|
||||
@ -402,3 +403,29 @@ class BlockContextTests(SimpleTestCase):
|
||||
"<BlockContext: blocks=defaultdict(<class 'list'>, "
|
||||
"{'content': [<Block Node: content. Contents: []>]})>",
|
||||
)
|
||||
|
||||
|
||||
class TemplateNameInExceptionTests(SimpleTestCase):
|
||||
template_error_msg = (
|
||||
"Invalid block tag on line 1: 'endfor'. Did you forget to register or "
|
||||
"load this tag?"
|
||||
)
|
||||
|
||||
def test_template_name_in_error_message(self):
|
||||
msg = f"Template: test.html, {self.template_error_msg}"
|
||||
with self.assertRaisesMessage(TemplateSyntaxError, msg):
|
||||
Template("{% endfor %}", origin=Origin("test.html"))
|
||||
|
||||
def test_template_name_not_in_debug_view(self):
|
||||
try:
|
||||
Template("{% endfor %}", origin=Origin("test.html"))
|
||||
except TemplateSyntaxError as e:
|
||||
reporter = ExceptionReporter(None, e.__class__, e, None)
|
||||
traceback_data = reporter.get_traceback_data()
|
||||
self.assertEqual(traceback_data["exception_value"], self.template_error_msg)
|
||||
|
||||
def test_unknown_source_template(self):
|
||||
try:
|
||||
Template("{% endfor %}")
|
||||
except TemplateSyntaxError as e:
|
||||
self.assertEqual(str(e), self.template_error_msg)
|
||||
|
Loading…
x
Reference in New Issue
Block a user