Skip to content

Commit 0c912be

Browse files
committed
Merge branch 'master' of github.com:KeepSafe/aiohttp
2 parents 759959f + 41a17d0 commit 0c912be

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

aiohttp/client_reqrep.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,12 @@ def release(self):
694694
self._connection = None
695695
self._cleanup_writer()
696696

697+
def raise_for_status(self):
698+
if 400 <= self.status:
699+
raise aiohttp.HttpProcessingError(
700+
code=self.status,
701+
message=self.reason)
702+
697703
def _cleanup_writer(self):
698704
if self._writer is not None and not self._writer.done():
699705
self._writer.cancel()

docs/client_reference.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,11 @@ Response object
11491149
return it into free connection pool for re-usage by next upcoming
11501150
request.
11511151

1152+
.. method:: raise_for_status()
1153+
1154+
Raise an HttpProcessingError if the response status is 400 or higher.
1155+
Do nothing for success responses (less than 400).
1156+
11521157
.. comethod:: text(encoding=None)
11531158

11541159
Read response's body and return decoded :class:`str` using

tests/test_client_response.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,16 @@ def test_close_deprecated(self):
274274
with self.assertWarns(DeprecationWarning):
275275
self.response.close(force=False)
276276
self.assertIsNone(self.response._connection)
277+
278+
def test_raise_for_status_2xx(self):
279+
self.response.status = 200
280+
self.response.reason = 'OK'
281+
self.response.raise_for_status() # should not raise
282+
283+
def test_raise_for_status_4xx(self):
284+
self.response.status = 409
285+
self.response.reason = 'CONFLICT'
286+
with self.assertRaises(aiohttp.HttpProcessingError) as cm:
287+
self.response.raise_for_status()
288+
self.assertEqual(str(cm.exception.code), '409')
289+
self.assertEqual(str(cm.exception.message), "CONFLICT")

0 commit comments

Comments
 (0)