From 1239499b5d6274f997a890650a516f6c5538a188 Mon Sep 17 00:00:00 2001 From: Burak Yigit Kaya Date: Fri, 13 Dec 2024 11:26:43 +0000 Subject: [PATCH] fix(spotlight): Make Django middleware init even more defensive (#3870) I just got faced with a situation where even trying to do `settings.DEBUG` may trigger a Django exception if the settings are not loaded yet, hence widening the `capture_internal_exceptions()` scope for this. --- sentry_sdk/spotlight.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sentry_sdk/spotlight.py b/sentry_sdk/spotlight.py index a94c691723..1555afb829 100644 --- a/sentry_sdk/spotlight.py +++ b/sentry_sdk/spotlight.py @@ -210,13 +210,13 @@ def setup_spotlight(options): if not isinstance(url, str): return None - if ( - settings is not None - and settings.DEBUG - and env_to_bool(os.environ.get("SENTRY_SPOTLIGHT_ON_ERROR", "1")) - and env_to_bool(os.environ.get("SENTRY_SPOTLIGHT_MIDDLEWARE", "1")) - ): - with capture_internal_exceptions(): + with capture_internal_exceptions(): + if ( + settings is not None + and settings.DEBUG + and env_to_bool(os.environ.get("SENTRY_SPOTLIGHT_ON_ERROR", "1")) + and env_to_bool(os.environ.get("SENTRY_SPOTLIGHT_MIDDLEWARE", "1")) + ): middleware = settings.MIDDLEWARE if DJANGO_SPOTLIGHT_MIDDLEWARE_PATH not in middleware: settings.MIDDLEWARE = type(middleware)(