Skip to content

Commit 4dc2deb

Browse files
authored
fix(django): Attempt custom urlconf resolve in got_request_exception as well (#1317)
1 parent 639c941 commit 4dc2deb

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

sentry_sdk/integrations/django/__init__.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
from django.http.request import QueryDict
5959
from django.utils.datastructures import MultiValueDict
6060

61+
from sentry_sdk.scope import Scope
6162
from sentry_sdk.integrations.wsgi import _ScopedResponse
6263
from sentry_sdk._types import Event, Hint, EventProcessor, NotImplementedType
6364

@@ -346,8 +347,8 @@ def _before_get_response(request):
346347
)
347348

348349

349-
def _after_get_response(request):
350-
# type: (WSGIRequest) -> None
350+
def _attempt_resolve_again(request, scope):
351+
# type: (WSGIRequest, Scope) -> None
351352
"""
352353
Some django middlewares overwrite request.urlconf
353354
so we need to respect that contract,
@@ -356,19 +357,24 @@ def _after_get_response(request):
356357
if not hasattr(request, "urlconf"):
357358
return
358359

360+
try:
361+
scope.transaction = LEGACY_RESOLVER.resolve(
362+
request.path_info,
363+
urlconf=request.urlconf,
364+
)
365+
except Exception:
366+
pass
367+
368+
369+
def _after_get_response(request):
370+
# type: (WSGIRequest) -> None
359371
hub = Hub.current
360372
integration = hub.get_integration(DjangoIntegration)
361373
if integration is None or integration.transaction_style != "url":
362374
return
363375

364376
with hub.configure_scope() as scope:
365-
try:
366-
scope.transaction = LEGACY_RESOLVER.resolve(
367-
request.path_info,
368-
urlconf=request.urlconf,
369-
)
370-
except Exception:
371-
pass
377+
_attempt_resolve_again(request, scope)
372378

373379

374380
def _patch_get_response():
@@ -431,6 +437,10 @@ def _got_request_exception(request=None, **kwargs):
431437
integration = hub.get_integration(DjangoIntegration)
432438
if integration is not None:
433439

440+
if request is not None and integration.transaction_style == "url":
441+
with hub.configure_scope() as scope:
442+
_attempt_resolve_again(request, scope)
443+
434444
# If an integration is there, a client has to be there.
435445
client = hub.client # type: Any
436446

tests/integrations/django/myapp/custom_urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ def path(path, *args, **kwargs):
2828

2929
urlpatterns = [
3030
path("custom/ok", views.custom_ok, name="custom_ok"),
31+
path("custom/exc", views.custom_exc, name="custom_exc"),
3132
]

tests/integrations/django/myapp/views.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ def custom_ok(request, *args, **kwargs):
125125
return HttpResponse("custom ok")
126126

127127

128+
@csrf_exempt
129+
def custom_exc(request, *args, **kwargs):
130+
1 / 0
131+
132+
128133
@csrf_exempt
129134
def template_test2(request, *args, **kwargs):
130135
return TemplateResponse(

tests/integrations/django/test_basic.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,17 @@ def test_custom_urlconf_middleware(
777777
assert status.lower() == "200 ok"
778778
assert b"".join(content) == b"custom ok"
779779

780-
(event,) = events
780+
event = events.pop(0)
781781
assert event["transaction"] == "/custom/ok"
782782
assert "custom_urlconf_middleware" in render_span_tree(event)
783783

784+
_content, status, _headers = client.get("/custom/exc")
785+
assert status.lower() == "500 internal server error"
786+
787+
error_event, transaction_event = events
788+
assert error_event["transaction"] == "/custom/exc"
789+
assert error_event["exception"]["values"][-1]["mechanism"]["type"] == "django"
790+
assert transaction_event["transaction"] == "/custom/exc"
791+
assert "custom_urlconf_middleware" in render_span_tree(transaction_event)
792+
784793
settings.MIDDLEWARE.pop(0)

0 commit comments

Comments
 (0)