Skip to content

Commit

Permalink
Merge pull request #1742 from penguinolog/history
Browse files Browse the repository at this point in the history
Implement history for ClientResponseError.
  • Loading branch information
fafhrd91 authored Mar 22, 2017
2 parents 0e99cfb + ae64ee6 commit ef0a3f0
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 2 deletions.
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: 4 additions & 0 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,20 +386,23 @@ def _ws_connect(self, url, *,
if resp.status != 101:
raise WSServerHandshakeError(
resp.request_info,
resp.history,
message='Invalid response status',
code=resp.status,
headers=resp.headers)

if resp.headers.get(hdrs.UPGRADE, '').lower() != 'websocket':
raise WSServerHandshakeError(
resp.request_info,
resp.history,
message='Invalid upgrade header',
code=resp.status,
headers=resp.headers)

if resp.headers.get(hdrs.CONNECTION, '').lower() != 'upgrade':
raise WSServerHandshakeError(
resp.request_info,
resp.history,
message='Invalid connection header',
code=resp.status,
headers=resp.headers)
Expand All @@ -411,6 +414,7 @@ def _ws_connect(self, url, *,
if key != match:
raise WSServerHandshakeError(
resp.request_info,
resp.history,
message='Invalid challenge response',
code=resp.status,
headers=resp.headers)
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, history, *,
code=0, message='', headers=None):
self.request_info = request_info
self.code = code
self.message = message
self.headers = headers
self.history = history

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

Expand Down
4 changes: 3 additions & 1 deletion aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def start(self, connection, read_until_eof=False):
(message, payload) = yield from self._protocol.read()
except http.HttpProcessingError as exc:
raise ClientResponseError(
self.request_info,
self.request_info, self.history,
code=exc.code,
message=exc.message, headers=exc.headers) from exc

Expand Down Expand Up @@ -632,6 +632,7 @@ def raise_for_status(self):
if 400 <= self.status:
raise ClientResponseError(
self.request_info,
self.history,
code=self.status,
message=self.reason,
headers=self.headers)
Expand Down Expand Up @@ -707,6 +708,7 @@ def json(self, *, encoding=None, loads=json.loads,
if content_type not in ctype:
raise ClientResponseError(
self.request_info,
self.history,
message=('Attempt to decode JSON with '
'unexpected mimetype: %s' % ctype),
headers=self.headers)
Expand Down
1 change: 1 addition & 0 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ def _create_proxy_connection(self, req):
if resp.status != 200:
raise ClientHttpProxyError(
proxy_resp.request_info,
resp.history,
code=resp.status,
message=resp.reason,
headers=resp.headers)
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

0 comments on commit ef0a3f0

Please sign in to comment.