From 83645a31157119900a9a71618bcd5820069ed8f6 Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Fri, 7 Aug 2020 19:36:12 -0700 Subject: [PATCH] sentry: Ignore SystemExit and similar exceptions. There are three exceptions in Python3 which are descended from BaseException, but not Exception: GeneratorExit, KeyboardInterrupt, and SystemExit. None of these are suitable to be sent to Sentry. For example, SystemExit is raised by `sys.exit`; in that sense, it is never "uncaught" because we chose to cause it explicitly. Use the suggested form[1] for ignoring specific classes of exceptions. [1] https://github.com/getsentry/sentry-python/issues/149#issuecomment-434448781 --- zproject/sentry.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/zproject/sentry.py b/zproject/sentry.py index bae8bc4da3..45de6a326d 100644 --- a/zproject/sentry.py +++ b/zproject/sentry.py @@ -12,7 +12,12 @@ from .config import PRODUCTION if TYPE_CHECKING: from sentry_sdk._types import Event, Hint -def add_context(event: 'Event', hint: 'Hint') -> 'Event': +def add_context(event: 'Event', hint: 'Hint') -> Optional['Event']: + if "exc_info" in hint: + _, exc_value, _ = hint["exc_info"] + # Ignore GeneratorExit, KeyboardInterrupt, and SystemExit exceptions + if not isinstance(exc_value, Exception): + return None from zerver.models import get_user_profile_by_id with capture_internal_exceptions(): user_info = event.get("user", {})