Skip to content

fix(api): Preserve RateLimitExceeded metadata in handle_query_errors#109821

Merged
cvxluo merged 1 commit intomasterfrom
cvxluo/zvkwywzxwpvs
Mar 4, 2026
Merged

fix(api): Preserve RateLimitExceeded metadata in handle_query_errors#109821
cvxluo merged 1 commit intomasterfrom
cvxluo/zvkwywzxwpvs

Conversation

@cvxluo
Copy link
Contributor

@cvxluo cvxluo commented Mar 3, 2026

Re-raise RateLimitExceeded instead of converting to Throttled in handle_query_errors(), so custom_exception_handler can capture the rate limit metadata (policy, quota_unit, storage_key, quota_used, rejection_threshold) onto the request for access log middleware.

No changes for the user, since custom_exception_handler will still reraise Throttled with the same error message for the frontend.

@cvxluo cvxluo requested a review from a team March 3, 2026 18:59
@cvxluo cvxluo marked this pull request as ready for review March 3, 2026 19:34
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Mar 4, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2026

Backend Test Failures

Failures on 637ea10 in this run:

tests/sentry/api/test_organization_events.py::OrganizationEventsEndpointTest::test_handling_snuba_errorslog
tests/sentry/api/test_organization_events.py:133: in test_handling_snuba_errors
    assert response.data["detail"] == constants.RATE_LIMIT_ERROR_MESSAGE
E   AssertionError: assert ErrorDetail(s...e='throttled') == '\nRate limit...r projects.\n'
E     
E     - 
E     - Rate limit exceeded. Please try your query with a smaller date range or fewer projects.
E     ?                                                                                        -
E     + Rate limit exceeded. Please try your query with a smaller date range or fewer projects.

Comment on lines 417 to +419
if isinstance(error, RateLimitExceeded):
sentry_sdk.set_tag("query.error_reason", "RateLimitExceeded")
raise Throttled(detail=RATE_LIMIT_ERROR_MESSAGE)
raise
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The change to re-raise RateLimitExceeded in handle_query_errors will cause background tasks like run_sdk_update_detector_for_organization to fail when they hit a rate limit.
Severity: MEDIUM

Suggested Fix

Consider reverting the behavior within handle_query_errors to catch RateLimitExceeded and raise a Throttled exception. Alternatively, add explicit try-except RateLimitExceeded blocks around calls to handle_query_errors in background tasks to handle this case gracefully.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: src/sentry/api/utils.py#L417-L419

Potential issue: The `handle_query_errors` context manager no longer converts
`RateLimitExceeded` exceptions into `Throttled` errors, but re-raises them. Background
tasks, such as `run_sdk_update_detector_for_organization`, use this context manager
without a surrounding `try-except` block. Consequently, if a rate limit is encountered
during a query within this task, the `RateLimitExceeded` exception will propagate
unhandled, causing the Celery task to fail. While the task framework will log this
failure and prevent a server crash, it represents a functional regression where tasks
fail instead of handling rate limits gracefully.

Did we get this right? 👍 / 👎 to inform future reviews.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2026

Backend Test Failures

Failures on 36c7a2a in this run:

tests/sentry/api/test_organization_events.py::OrganizationEventsEndpointTest::test_handling_snuba_errorslog
tests/sentry/api/test_organization_events.py:133: in test_handling_snuba_errors
    assert response.data["detail"] == constants.RATE_LIMIT_ERROR_MESSAGE
E   AssertionError: assert ErrorDetail(s...e='throttled') == '\nRate limit...r projects.\n'
E     
E     - 
E     - Rate limit exceeded. Please try your query with a smaller date range or fewer projects.
E     ?                                                                                        -
E     + Rate limit exceeded. Please try your query with a smaller date range or fewer projects.

Copy link
Contributor

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2026

Backend Test Failures

Failures on 551ef80 in this run:

tests/sentry/api/test_handlers.py::TestRateLimited::test_simplelog
tests/sentry/api/test_handlers.py:33: in test_simple
    assert (
E   AssertionError: assert ErrorDetail(s...e='throttled') == 'Rate limit e...wer projects.'
E     
E     + 
E     - Rate limit exceeded. Please try your query with a smaller date range or fewer projects.
E     + Rate limit exceeded. Please try your query with a smaller date range or fewer projects.
E     ?                                                                                        +

Re-raise RateLimitExceeded instead of converting to Throttled in
handle_query_errors(), so custom_exception_handler can capture the
rate limit metadata (policy, quota_unit, storage_key, quota_used,
rejection_threshold) onto the request for access log middleware.


Co-authored-by: Claude <noreply@anthropic.com>
@cvxluo cvxluo force-pushed the cvxluo/zvkwywzxwpvs branch from 1bc6ad1 to b6b5859 Compare March 4, 2026 18:31
@cvxluo cvxluo merged commit f55826d into master Mar 4, 2026
76 checks passed
@cvxluo cvxluo deleted the cvxluo/zvkwywzxwpvs branch March 4, 2026 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants