Skip to content

Commit

Permalink
fix: Django template frame in correct position, fix #281
Browse files Browse the repository at this point in the history
  • Loading branch information
untitaker committed Mar 5, 2019
1 parent abff4eb commit 4cbc1cf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
18 changes: 14 additions & 4 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,20 @@ def process_django_templates(event, hint):
):
frame = get_template_frame_from_exception(exc_value)
if frame is not None:
frames = exception.setdefault("stacktrace", {}).setdefault(
"frames", []
)
frames.append(frame)
frames = exception.get("stacktrace", {}).get("frames", [])

for i in reversed(range(len(frames))):
f = frames[i]
if (
f.get("function") in ("parse", "render")
and f.get("module") == "django.template.base"
):
i += 1
break
else:
i = len(frames)

frames.insert(i, frame)

return event

Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/django/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def _get_template_frame_from_debug(debug):
"pre_context": pre_context[-5:],
"post_context": post_context[:5],
"context_line": context_line,
"in_app": True,
}


Expand Down
10 changes: 10 additions & 0 deletions tests/integrations/django/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,13 @@ def test_template_exception(sentry_init, client, capture_events):
assert template_frame["lineno"] == 10
assert template_frame["in_app"]
assert template_frame["filename"].endswith("error.html")

filenames = [
(f.get("function"), f.get("module")) for f in exception["stacktrace"]["frames"]
]
assert filenames[1] == (u"template_exc", u"tests.integrations.django.myapp.views")
assert filenames[-3:] == [
(u"parse", u"django.template.base"),
(None, None),
(u"invalid_block_tag", u"django.template.base"),
]

0 comments on commit 4cbc1cf

Please sign in to comment.