Skip to content

Commit 2c27210

Browse files
committed
Handle ValueError in scope resets
1 parent a311e3b commit 2c27210

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

sentry_sdk/scope.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,7 @@ def new_scope():
16791679
try:
16801680
# restore original scope
16811681
_current_scope.reset(token)
1682-
except LookupError:
1682+
except (LookupError, ValueError):
16831683
capture_internal_exception(sys.exc_info())
16841684

16851685

@@ -1717,7 +1717,7 @@ def use_scope(scope):
17171717
try:
17181718
# restore original scope
17191719
_current_scope.reset(token)
1720-
except LookupError:
1720+
except (LookupError, ValueError):
17211721
capture_internal_exception(sys.exc_info())
17221722

17231723

@@ -1761,12 +1761,12 @@ def isolation_scope():
17611761
# restore original scopes
17621762
try:
17631763
_current_scope.reset(current_token)
1764-
except LookupError:
1764+
except (LookupError, ValueError):
17651765
capture_internal_exception(sys.exc_info())
17661766

17671767
try:
17681768
_isolation_scope.reset(isolation_token)
1769-
except LookupError:
1769+
except (LookupError, ValueError):
17701770
capture_internal_exception(sys.exc_info())
17711771

17721772

@@ -1808,12 +1808,12 @@ def use_isolation_scope(isolation_scope):
18081808
# restore original scopes
18091809
try:
18101810
_current_scope.reset(current_token)
1811-
except LookupError:
1811+
except (LookupError, ValueError):
18121812
capture_internal_exception(sys.exc_info())
18131813

18141814
try:
18151815
_isolation_scope.reset(isolation_token)
1816-
except LookupError:
1816+
except (LookupError, ValueError):
18171817
capture_internal_exception(sys.exc_info())
18181818

18191819

tests/test_scope.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -908,17 +908,18 @@ def test_last_event_id_cleared(sentry_init):
908908

909909

910910
@pytest.mark.tests_internal_exceptions
911+
@pytest.mark.parametrize("error_cls", [LookupError, ValueError])
911912
@pytest.mark.parametrize(
912913
"scope_manager",
913914
[
914915
new_scope,
915916
use_scope,
916917
],
917918
)
918-
def test_handle_lookup_error_on_token_reset_current_scope(scope_manager):
919+
def test_handle_error_on_token_reset_current_scope(error_cls, scope_manager):
919920
with mock.patch("sentry_sdk.scope.capture_internal_exception") as mock_capture:
920921
with mock.patch("sentry_sdk.scope._current_scope") as mock_token_var:
921-
mock_token_var.reset.side_effect = LookupError()
922+
mock_token_var.reset.side_effect = error_cls()
922923

923924
mock_token = mock.Mock()
924925
mock_token_var.set.return_value = mock_token
@@ -932,27 +933,28 @@ def test_handle_lookup_error_on_token_reset_current_scope(scope_manager):
932933
pass
933934

934935
except Exception:
935-
pytest.fail("Context manager should handle LookupError gracefully")
936+
pytest.fail(f"Context manager should handle {error_cls} gracefully")
936937

937938
mock_capture.assert_called_once()
938939
mock_token_var.reset.assert_called_once_with(mock_token)
939940

940941

941942
@pytest.mark.tests_internal_exceptions
943+
@pytest.mark.parametrize("error_cls", [LookupError, ValueError])
942944
@pytest.mark.parametrize(
943945
"scope_manager",
944946
[
945947
isolation_scope,
946948
use_isolation_scope,
947949
],
948950
)
949-
def test_handle_lookup_error_on_token_reset_isolation_scope(scope_manager):
951+
def test_handle_error_on_token_reset_isolation_scope(error_cls, scope_manager):
950952
with mock.patch("sentry_sdk.scope.capture_internal_exception") as mock_capture:
951953
with mock.patch("sentry_sdk.scope._current_scope") as mock_current_scope:
952954
with mock.patch(
953955
"sentry_sdk.scope._isolation_scope"
954956
) as mock_isolation_scope:
955-
mock_isolation_scope.reset.side_effect = LookupError()
957+
mock_isolation_scope.reset.side_effect = error_cls()
956958
mock_current_token = mock.Mock()
957959
mock_current_scope.set.return_value = mock_current_token
958960

@@ -965,7 +967,7 @@ def test_handle_lookup_error_on_token_reset_isolation_scope(scope_manager):
965967
pass
966968

967969
except Exception:
968-
pytest.fail("Context manager should handle LookupError gracefully")
970+
pytest.fail(f"Context manager should handle {error_cls} gracefully")
969971

970972
mock_capture.assert_called_once()
971973
mock_current_scope.reset.assert_called_once_with(mock_current_token)

0 commit comments

Comments
 (0)