Skip to content

Commit

Permalink
Merge branch 'master' of github.com:KeepSafe/aiohttp
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Jun 3, 2016
2 parents 759959f + 41a17d0 commit 0c912be
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,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 @@ -1149,6 +1149,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")

0 comments on commit 0c912be

Please sign in to comment.