diff --git a/sentry_sdk/integrations/django/__init__.py b/sentry_sdk/integrations/django/__init__.py index 4248a0652c..8419f56df0 100644 --- a/sentry_sdk/integrations/django/__init__.py +++ b/sentry_sdk/integrations/django/__init__.py @@ -4,6 +4,7 @@ import sys import threading import weakref +from importlib import import_module from sentry_sdk._types import TYPE_CHECKING from sentry_sdk.consts import OP, SPANDATA @@ -32,6 +33,8 @@ from django import VERSION as DJANGO_VERSION from django.conf import settings as django_settings from django.core import signals + from django.urls.exceptions import Resolver404 + from django.conf import settings try: from django.urls import resolve @@ -370,6 +373,15 @@ def _set_transaction_name_and_source(scope, transaction_style, request): transaction_name, source=source, ) + except Resolver404: + urlconf = import_module(settings.ROOT_URLCONF) + # This exception only gets thrown when transaction_style is `function_name` + # So we don't check here what style is configured + if hasattr(urlconf, "handler404"): + handler = getattr(urlconf, "handler404") + scope.transaction = transaction_from_function( + getattr(handler, "view_class", handler) + ) except Exception: pass