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

Add client_resp.raise_for_status() #908

Merged
merged 1 commit into from
Jun 3, 2016
Merged
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
6 changes: 6 additions & 0 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,12 @@ def release(self):
self._connection = None
self._cleanup_writer()

def raise_for_status(self):
if 400 <= self.status:
raise aiohttp.HttpProcessingError(
code=self.status,
message=self.reason)

def _cleanup_writer(self):
if self._writer is not None and not self._writer.done():
self._writer.cancel()
Expand Down
5 changes: 5 additions & 0 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,11 @@ Response object
return it into free connection pool for re-usage by next upcoming
request.

.. method:: raise_for_status()

Raise an HttpProcessingError if the response status is 400 or higher.
Do nothing for success responses (less than 400).

.. comethod:: text(encoding=None)

Read response's body and return decoded :class:`str` using
Expand Down
13 changes: 13 additions & 0 deletions tests/test_client_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,16 @@ def test_close_deprecated(self):
with self.assertWarns(DeprecationWarning):
self.response.close(force=False)
self.assertIsNone(self.response._connection)

def test_raise_for_status_2xx(self):
self.response.status = 200
self.response.reason = 'OK'
self.response.raise_for_status() # should not raise

def test_raise_for_status_4xx(self):
self.response.status = 409
self.response.reason = 'CONFLICT'
with self.assertRaises(aiohttp.HttpProcessingError) as cm:
self.response.raise_for_status()
self.assertEqual(str(cm.exception.code), '409')
self.assertEqual(str(cm.exception.message), "CONFLICT")