Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,7 @@ def new_scope():
try:
# restore original scope
_current_scope.reset(token)
except LookupError:
except (LookupError, ValueError):
capture_internal_exception(sys.exc_info())


Expand Down Expand Up @@ -1717,7 +1717,7 @@ def use_scope(scope):
try:
# restore original scope
_current_scope.reset(token)
except LookupError:
except (LookupError, ValueError):
capture_internal_exception(sys.exc_info())


Expand Down Expand Up @@ -1761,12 +1761,12 @@ def isolation_scope():
# restore original scopes
try:
_current_scope.reset(current_token)
except LookupError:
except (LookupError, ValueError):
capture_internal_exception(sys.exc_info())

try:
_isolation_scope.reset(isolation_token)
except LookupError:
except (LookupError, ValueError):
capture_internal_exception(sys.exc_info())


Expand Down Expand Up @@ -1808,12 +1808,12 @@ def use_isolation_scope(isolation_scope):
# restore original scopes
try:
_current_scope.reset(current_token)
except LookupError:
except (LookupError, ValueError):
capture_internal_exception(sys.exc_info())

try:
_isolation_scope.reset(isolation_token)
except LookupError:
except (LookupError, ValueError):
capture_internal_exception(sys.exc_info())


Expand Down
14 changes: 8 additions & 6 deletions tests/test_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,17 +908,18 @@ def test_last_event_id_cleared(sentry_init):


@pytest.mark.tests_internal_exceptions
@pytest.mark.parametrize("error_cls", [LookupError, ValueError])
@pytest.mark.parametrize(
"scope_manager",
[
new_scope,
use_scope,
],
)
def test_handle_lookup_error_on_token_reset_current_scope(scope_manager):
def test_handle_error_on_token_reset_current_scope(error_cls, scope_manager):
with mock.patch("sentry_sdk.scope.capture_internal_exception") as mock_capture:
with mock.patch("sentry_sdk.scope._current_scope") as mock_token_var:
mock_token_var.reset.side_effect = LookupError()
mock_token_var.reset.side_effect = error_cls()

mock_token = mock.Mock()
mock_token_var.set.return_value = mock_token
Expand All @@ -932,27 +933,28 @@ def test_handle_lookup_error_on_token_reset_current_scope(scope_manager):
pass

except Exception:
pytest.fail("Context manager should handle LookupError gracefully")
pytest.fail(f"Context manager should handle {error_cls} gracefully")

mock_capture.assert_called_once()
mock_token_var.reset.assert_called_once_with(mock_token)


@pytest.mark.tests_internal_exceptions
@pytest.mark.parametrize("error_cls", [LookupError, ValueError])
@pytest.mark.parametrize(
"scope_manager",
[
isolation_scope,
use_isolation_scope,
],
)
def test_handle_lookup_error_on_token_reset_isolation_scope(scope_manager):
def test_handle_error_on_token_reset_isolation_scope(error_cls, scope_manager):
with mock.patch("sentry_sdk.scope.capture_internal_exception") as mock_capture:
with mock.patch("sentry_sdk.scope._current_scope") as mock_current_scope:
with mock.patch(
"sentry_sdk.scope._isolation_scope"
) as mock_isolation_scope:
mock_isolation_scope.reset.side_effect = LookupError()
mock_isolation_scope.reset.side_effect = error_cls()
mock_current_token = mock.Mock()
mock_current_scope.set.return_value = mock_current_token

Expand All @@ -965,7 +967,7 @@ def test_handle_lookup_error_on_token_reset_isolation_scope(scope_manager):
pass

except Exception:
pytest.fail("Context manager should handle LookupError gracefully")
pytest.fail(f"Context manager should handle {error_cls} gracefully")

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