Skip to content

Commit

Permalink
Turns out, rate limiting is it's own exception
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrobenolt committed Apr 15, 2014
1 parent ea53ddb commit 6db3d8d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
8 changes: 3 additions & 5 deletions raven/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import raven
from raven.conf import defaults
from raven.context import Context
from raven.exceptions import APIError
from raven.exceptions import APIError, RateLimited
from raven.utils import six, json, get_versions, get_auth_header, merge_dicts
from raven.utils.encoding import to_unicode
from raven.utils.serializer import transform
Expand Down Expand Up @@ -527,13 +527,11 @@ def _successful_send(self):
def _failed_send(self, e, url, data):
retry_after = 0
if isinstance(e, APIError):
if isinstance(e, RateLimited):
retry_after = e.retry_after
self.error_logger.error('Unable to capture event: %s', e.message)
elif isinstance(e, HTTPError):
body = e.read()
try:
retry_after = int(e.headers.get('Retry-After'))
except (ValueError, TypeError):
pass
self.error_logger.error(
'Unable to reach Sentry log server: %s (url: %s, body: %s)',
e, url, body, exc_info=True,
Expand Down
4 changes: 3 additions & 1 deletion raven/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ def __unicode__(self):


class RateLimited(APIError):
pass
def __init__(self, message, retry_after=0):
self.retry_after = retry_after
super(RateLimited, self).__init__(message, 429)
3 changes: 2 additions & 1 deletion raven/transport/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def send(self, data, headers):
msg = exc.headers.get('x-sentry-error')
code = exc.getcode()
if code == 429:
raise RateLimited(msg, code)
retry_after = int(exc.headers.get('retry-after', 0))
raise RateLimited(msg, retry_after)
elif msg:
raise APIError(msg, code)
else:
Expand Down
8 changes: 2 additions & 6 deletions tests/base/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import time
from socket import socket, AF_INET, SOCK_DGRAM
from raven.base import Client, ClientState
from raven.exceptions import RateLimited
from raven.transport import AsyncTransport
from raven.utils.stacks import iter_stack_frames
from raven.utils import six
from raven.utils.testutils import TestCase
from raven.utils.compat import HTTPError


class TempStoreClient(Client):
Expand Down Expand Up @@ -122,12 +122,8 @@ def test_send_remote_failover_with_retry_after(self, should_try, send):
dsn='sync+http://public:[email protected]/1'
)

e = HTTPError(
'http://example.com/api/store', 429, 'oops',
{'Retry-After': '5'}, six.StringIO())

# test error
send.side_effect = e
send.side_effect = RateLimited('foo', 5)
client.send_remote('sync+http://example.com/api/store', 'foo')
self.assertEquals(client.state.status, client.state.ERROR)
self.assertEqual(client.state.retry_after, 5)
Expand Down

0 comments on commit 6db3d8d

Please sign in to comment.