Skip to content

Commit 684c43f

Browse files
BeryJusentrivanaantonpirker
authored
Django: Fix 404 Handler handler being labeled as "generic ASGI request" (#1277)
* fix(django): Fix 404 Handler handler being labeled as "generic ASGI request" --------- Co-authored-by: Ivana Kellyerova <[email protected]> Co-authored-by: Anton Pirker <[email protected]>
1 parent a03e78f commit 684c43f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

sentry_sdk/integrations/django/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import sys
55
import threading
66
import weakref
7+
from importlib import import_module
78

9+
from sentry_sdk._compat import string_types
810
from sentry_sdk._types import TYPE_CHECKING
911
from sentry_sdk.consts import OP, SPANDATA
1012
from sentry_sdk.hub import Hub, _should_send_default_pii
@@ -32,11 +34,17 @@
3234
from django import VERSION as DJANGO_VERSION
3335
from django.conf import settings as django_settings
3436
from django.core import signals
37+
from django.conf import settings
3538

3639
try:
3740
from django.urls import resolve
3841
except ImportError:
3942
from django.core.urlresolvers import resolve
43+
44+
try:
45+
from django.urls import Resolver404
46+
except ImportError:
47+
from django.core.urlresolvers import Resolver404
4048
except ImportError:
4149
raise DidNotEnable("Django not installed")
4250

@@ -370,6 +378,18 @@ def _set_transaction_name_and_source(scope, transaction_style, request):
370378
transaction_name,
371379
source=source,
372380
)
381+
except Resolver404:
382+
urlconf = import_module(settings.ROOT_URLCONF)
383+
# This exception only gets thrown when transaction_style is `function_name`
384+
# So we don't check here what style is configured
385+
if hasattr(urlconf, "handler404"):
386+
handler = urlconf.handler404
387+
if isinstance(handler, string_types):
388+
scope.transaction = handler
389+
else:
390+
scope.transaction = transaction_from_function(
391+
getattr(handler, "view_class", handler)
392+
)
373393
except Exception:
374394
pass
375395

0 commit comments

Comments
 (0)