Skip to content

Commit eb81676

Browse files
committed
chore: refactor try-except blocks into contextmgr
1 parent 94ce9b4 commit eb81676

5 files changed

Lines changed: 19 additions & 21 deletions

File tree

sentry_sdk/hub.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
_local = ContextVar("sentry_current_hub")
1111

1212

13+
@contextmanager
14+
def _internal_exceptions():
15+
try:
16+
yield
17+
except Exception:
18+
Hub.current.capture_exception()
19+
20+
1321
class HubMeta(type):
1422
@property
1523
def current(self):

sentry_sdk/integrations/celery.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from celery.signals import task_failure, task_prerun, task_postrun
66

77
from sentry_sdk import get_current_hub, configure_scope, capture_exception
8+
from sentry_sdk.hub import _internal_exceptions
89

910

1011
_installer_lock = Lock()
@@ -32,13 +33,11 @@ def _process_failure_signal(sender, task_id, einfo, **kw):
3233

3334

3435
def _handle_task_prerun(sender, task, **kw):
35-
try:
36+
with _internal_exceptions():
3637
get_current_hub().push_scope()
3738

3839
with configure_scope() as scope:
3940
scope.transaction = task.name
40-
except Exception:
41-
get_current_hub().capture_internal_exception()
4241

4342

4443
def _handle_task_postrun(sender, task_id, task, **kw):

sentry_sdk/integrations/django/__init__.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from django.core.urlresolvers import resolve
1212

1313
from sentry_sdk import get_current_hub, configure_scope, capture_exception
14+
from sentry_sdk.hub import _internal_exceptions
1415
from .._wsgi import RequestExtractor
1516

1617

@@ -31,7 +32,7 @@ def _get_transaction_from_request(request):
3132
# not yet available
3233
class SentryMiddleware(MiddlewareMixin):
3334
def process_request(self, request):
34-
try:
35+
with _internal_exceptions():
3536
get_current_hub().push_scope()
3637

3738
get_current_hub().add_event_processor(
@@ -40,15 +41,11 @@ def process_request(self, request):
4041

4142
with configure_scope() as scope:
4243
scope.transaction = _get_transaction_from_request(request)
43-
except Exception:
44-
get_current_hub().capture_internal_exception()
4544

4645
def make_event_processor(self, request):
4746
def processor(event):
48-
try:
47+
with _internal_exceptions():
4948
DjangoRequestExtractor(request).extract_into_event(event)
50-
except Exception:
51-
get_current_hub().capture_internal_exception()
5249

5350
# TODO: user info
5451

sentry_sdk/integrations/flask.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from threading import Lock
44

55
from sentry_sdk import capture_exception, get_current_hub
6+
from sentry_sdk.hub import _internal_exceptions
67
from ._wsgi import RequestExtractor
78

89
try:
@@ -51,20 +52,14 @@ def _capture_exception(sender, exception, **kwargs):
5152
def _event_processor(event):
5253
if request:
5354
if "transaction" not in event:
54-
try:
55+
with _internal_exceptions():
5556
event["transaction"] = request.url_rule.endpoint
56-
except Exception:
57-
get_current_hub().capture_internal_exception()
5857

59-
try:
58+
with _internal_exceptions():
6059
FlaskRequestExtractor(request).extract_into_event(event)
61-
except Exception:
62-
get_current_hub().capture_internal_exception()
6360

64-
try:
61+
with _internal_exceptions():
6562
_set_user_info(event)
66-
except Exception:
67-
get_current_hub().capture_internal_exception()
6863

6964

7065
class FlaskRequestExtractor(RequestExtractor):

sentry_sdk/integrations/logging.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from sentry_sdk import get_current_hub, capture_event, add_breadcrumb
99
from sentry_sdk.utils import to_string, Event, skip_internal_frames
10+
from sentry_sdk.hub import _internal_exceptions
1011

1112

1213
_installer_lock = Lock()
@@ -37,11 +38,9 @@ def sentry_patched_callhandlers(self, record):
3738

3839
class SentryHandler(logging.Handler, object):
3940
def emit(self, record):
40-
try:
41+
with _internal_exceptions():
4142
self.format(record)
4243
return self._emit(record)
43-
except Exception:
44-
get_current_hub().capture_internal_exception()
4544

4645
def can_record(self, record):
4746
return not record.name.startswith("sentry_sdk")

0 commit comments

Comments
 (0)