Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): better error message return to the client #86155

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 12 additions & 3 deletions src/sentry/api/handlers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sentry_sdk
from rest_framework.exceptions import Throttled
from rest_framework.views import exception_handler

Expand All @@ -6,8 +7,16 @@

def custom_exception_handler(exc, context):
if isinstance(exc, RateLimitExceeded):
# If Snuba throws a RateLimitExceeded then it'll likely be available
# after another second.
exc = Throttled(wait=1)
# capture the rate limited exception so we can see it in Sentry
with sentry_sdk.new_scope() as scope:
scope.fingerprint = ["snuba-api-rate-limit-exceeded"]
sentry_sdk.capture_exception(
exc,
level="warning",
)
# let the client know that they've been rate limited with details
exc = Throttled(
detail="Rate limit exceeded. Please try your query with a smaller date range or fewer projects."
)

return exception_handler(exc, context)
9 changes: 7 additions & 2 deletions tests/sentry/api/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class RateLimitedEndpoint(Endpoint):
permission_classes = (AllowAny,)

def get(self, request):
raise RateLimitExceeded()
raise RateLimitExceeded(
"Rate limit exceeded. Please try your query with a smaller date range or fewer projects."
)


urlpatterns = [re_path(r"^/$", RateLimitedEndpoint.as_view(), name="sentry-test")]
Expand All @@ -28,4 +30,7 @@ def test_simple(self):
resp = self.get_response()
assert resp.status_code == 429

assert resp.data["detail"] == "Request was throttled. Expected available in 1 second."
assert (
resp.data["detail"]
== "Rate limit exceeded. Please try your query with a smaller date range or fewer projects."
)
Loading