Skip to content

Commit

Permalink
Merge pull request #41 from levi-rs/retry-503-errors
Browse files Browse the repository at this point in the history
Update exception raising and handling
  • Loading branch information
levi-rs committed Aug 8, 2017
2 parents 8909f24 + b0cafec commit 3fb318b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
20 changes: 16 additions & 4 deletions tests/test_sessions.py
Expand Up @@ -25,8 +25,20 @@ def session():


@pytest.fixture()
def response():
yield mock.create_autospec(requests.models.Response)
def request():
req = mock.create_autospec(requests.models.Request)
req.path_url = 'mock path url'
req.body = {'mock': 'stuff'}
yield req


@pytest.fixture()
def response(request):
resp = mock.create_autospec(requests.models.Response)
resp.request = request
resp.content = '{"error": "Error reason"}'
resp.reason = 'Response Reason'
yield resp


def test___init__():
Expand Down Expand Up @@ -209,10 +221,10 @@ def test_req_w_retries_service_unavailable(make_req_mock, session, response):
response.status_code = codes['service_unavailable']
make_req_mock.return_value = response, None

with pytest.raises(exceptions.ServerError):
with pytest.raises(exceptions.ServiceUnavailableError):
session._request_with_retries()

assert make_req_mock.call_count == 3
assert make_req_mock.call_count == 17


@mock.patch.object(Session, '_make_request')
Expand Down
16 changes: 14 additions & 2 deletions traw/exceptions.py
Expand Up @@ -55,9 +55,17 @@ def __init__(self, response):
:param response: A requests.response instance.
"""
msg = ('Received {0} ({1}) HTTP response\n'
'With message: {2}\n'
'For request: {3}')
msg = msg.format(response.status_code, response.reason,
response.content, response.request.path_url)

if hasattr(response.request, 'body') and response.request.body:
msg += '\nWith Request Body: {0}'.format(response.request.body)

self.response = response
super(ResponseException, self).__init__('received {} HTTP response'
.format(response.status_code))
super(ResponseException, self).__init__(msg)


class BadRequest(ResponseException):
Expand Down Expand Up @@ -129,6 +137,10 @@ class ServerError(ResponseException):
"""Indicate issues on the server end preventing request fulfillment."""


class ServiceUnavailableError(ResponseException):
"""Indicate issues on the server end preventing request fulfillment."""


class TooLarge(ResponseException):
"""Indicate that the request data exceeds the allowed limit."""

Expand Down
6 changes: 4 additions & 2 deletions traw/sessions.py
Expand Up @@ -9,7 +9,8 @@

from .const import BASE_API_PATH, TIMEOUT
from .exceptions import (BadRequest, Conflict, Forbidden, NotFound, RateLimited,
Redirect, ServerError, TooLarge, UnknownStatusCode)
Redirect, ServerError, ServiceUnavailableError,
TooLarge, UnknownStatusCode)

log = logging.getLogger(__package__)

Expand All @@ -27,7 +28,7 @@ class Session(object):
codes['internal_server_error']: ServerError,
codes['not_found']: NotFound,
codes['request_entity_too_large']: TooLarge,
codes['service_unavailable']: ServerError,
codes['service_unavailable']: ServiceUnavailableError,
codes['unauthorized']: Forbidden}
SUCCESS_STATUSES = {codes['created'], codes['ok']}

Expand Down Expand Up @@ -66,6 +67,7 @@ def _make_request(self, *args, **kwargs):
else:
return response, None

@retry(ServiceUnavailableError, tries=17, delay=1, backoff=2)
@retry(RETRY_EXCEPTIONS, tries=3, delay=1, backoff=2)
def _request_with_retries(self, *args, **kwargs):
""" """
Expand Down

0 comments on commit 3fb318b

Please sign in to comment.