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

Implement history for ClientResponseError. #1742

Merged
merged 3 commits into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Changes

- Dropped "%O" in access logger #1673

- Added `history` to `ClientResponseError`. #1741


2.0.2 (2017-03-21)
------------------
Expand Down
4 changes: 3 additions & 1 deletion aiohttp/client_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ class ClientResponseError(ClientError):
:param request_info: instance of RequestInfo
"""

def __init__(self, request_info, *, code=0, message='', headers=None):
def __init__(self, request_info, *, code=0, message='', headers=None,
history=None):
Copy link
Member

Choose a reason for hiding this comment

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

let's make it required. we always pass history.

self.request_info = request_info
self.code = code
self.message = message
self.headers = headers
self.history = history if history is not None else ()

super().__init__("%s, message='%s'" % (code, message))

Expand Down
9 changes: 6 additions & 3 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,8 @@ def start(self, connection, read_until_eof=False):
raise ClientResponseError(
self.request_info,
code=exc.code,
message=exc.message, headers=exc.headers) from exc
message=exc.message, headers=exc.headers,
history=self.history) from exc

if (message.code < 100 or
message.code > 199 or message.code == 101):
Expand Down Expand Up @@ -634,7 +635,8 @@ def raise_for_status(self):
self.request_info,
code=self.status,
message=self.reason,
headers=self.headers)
headers=self.headers,
history=self.history)

def _cleanup_writer(self):
if self._writer is not None and not self._writer.done():
Expand Down Expand Up @@ -709,7 +711,8 @@ def json(self, *, encoding=None, loads=json.loads,
self.request_info,
message=('Attempt to decode JSON with '
'unexpected mimetype: %s' % ctype),
headers=self.headers)
headers=self.headers,
history=self.history)

stripped = self._content.strip()
if not stripped:
Expand Down
3 changes: 3 additions & 0 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,9 @@ Hierarchy of exceptions:
.. attribute:: request_info
Instance of `RequestInfo` object, contains information about request.

.. attribute:: history
History from `ClientResponse` object, if available, else empty tuple.

* `WSServerHandshakeError` - web socket server response error

- `ClientHttpProxyError` - proxy response
Expand Down
58 changes: 58 additions & 0 deletions tests/test_client_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,61 @@ def test_request_info_in_exception():
with pytest.raises(aiohttp.ClientResponseError) as cm:
response.raise_for_status()
assert cm.value.request_info == response.request_info


def test_no_redirect_history_in_exception():
url = 'http://def-cl-resp.org'
headers = {'Content-Type': 'application/json;charset=cp1251'}
response = ClientResponse(
'get',
URL(url),
request_info=RequestInfo(
url,
'get',
headers
)
)
response.status = 409
response.reason = 'CONFLICT'
with pytest.raises(aiohttp.ClientResponseError) as cm:
response.raise_for_status()
assert () == cm.value.history


def test_redirect_history_in_exception():
hist_url = 'http://def-cl-resp.org'
url = 'http://def-cl-resp.org/index.htm'
hist_headers = {'Content-Type': 'application/json;charset=cp1251',
'Location': url
}
headers = {'Content-Type': 'application/json;charset=cp1251'}
response = ClientResponse(
'get',
URL(url),
request_info=RequestInfo(
url,
'get',
headers
)
)
response.status = 409
response.reason = 'CONFLICT'

hist_response = ClientResponse(
'get',
URL(hist_url),
request_info=RequestInfo(
url,
'get',
headers
)
)

hist_response.headers = hist_headers
hist_response.status = 301
hist_response.reason = 'REDIRECT'

response._history = [hist_response]
with pytest.raises(aiohttp.ClientResponseError) as cm:
response.raise_for_status()
assert [hist_response] == cm.value.history