fix(api): Preserve RateLimitExceeded metadata in handle_query_errors#109821
fix(api): Preserve RateLimitExceeded metadata in handle_query_errors#109821
Conversation
Backend Test FailuresFailures on
|
| if isinstance(error, RateLimitExceeded): | ||
| sentry_sdk.set_tag("query.error_reason", "RateLimitExceeded") | ||
| raise Throttled(detail=RATE_LIMIT_ERROR_MESSAGE) | ||
| raise |
There was a problem hiding this comment.
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.
Backend Test FailuresFailures on
|
78b594f to
1bc6ad1
Compare
There was a problem hiding this comment.
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.
Backend Test FailuresFailures on
|
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>
1bc6ad1 to
b6b5859
Compare
Re-raise RateLimitExceeded instead of converting to Throttled in handle_query_errors(), so
custom_exception_handlercan 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_handlerwill still reraiseThrottledwith the same error message for the frontend.