Skip to content

Commit

Permalink
Merge branch 'httperror_init' of git://github.com/dmedvinsky/requests
Browse files Browse the repository at this point in the history
Conflicts:
	AUTHORS.rst
	test_requests.py
  • Loading branch information
Kenneth Reitz committed Mar 4, 2013
2 parents b14584f + c4f9340 commit c0d4b23
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
2 changes: 2 additions & 0 deletions AUTHORS.rst
Expand Up @@ -121,3 +121,5 @@ Patches and Suggestions
- Vinod Chandru
- Johnny Goodnow <j.goodnow29@gmail.com>
- Denis Ryzhkov <denisr@denisr.com>
- Wilfred Hughes <me@wilfred.me.uk> @dontYetKnow
- Dmitry Medvinsky <me@dmedvinsky.name>
6 changes: 5 additions & 1 deletion requests/exceptions.py
Expand Up @@ -16,7 +16,11 @@ class RequestException(RuntimeError):

class HTTPError(RequestException):
"""An HTTP error occurred."""
response = None

def __init__(self, *args, **kwargs):
""" Initializes HTTPError with optional `response` object. """
self.response = kwargs.pop('response', None)
super(HTTPError, self).__init__(*args, **kwargs)


class ConnectionError(RequestException):
Expand Down
4 changes: 1 addition & 3 deletions requests/models.py
Expand Up @@ -657,9 +657,7 @@ def raise_for_status(self):
http_error_msg = '%s Server Error: %s' % (self.status_code, self.reason)

if http_error_msg:
http_error = HTTPError(http_error_msg)
http_error.response = self
raise http_error
raise HTTPError(http_error_msg, response=self)

def close(self):
return self.raw.release_conn()
31 changes: 31 additions & 0 deletions test_requests.py
Expand Up @@ -366,5 +366,36 @@ def test_cannot_send_unprepared_requests(self):
r = requests.Request(url=HTTPBIN)
self.assertRaises(ValueError, requests.Session().send, r)

def test_can_specify_retries(self):
# monkey patch urlopen
from requests.packages.urllib3.poolmanager import HTTPConnectionPool
old_urlopen = HTTPConnectionPool.urlopen

max_retries_used = []
def urlopen(*args, **kwargs):
"""Save what value we used for retries each time we call urlopen."""
max_retries_used.append(kwargs.get('retries'))
return old_urlopen(*args, **kwargs)

HTTPConnectionPool.urlopen = urlopen

# do the request and check that max_retries was passed through
requests.get(httpbin('get'), max_retries=5)
self.assertEqual(max_retries_used, [5])

# undo monkey patch
HTTPConnectionPool.urlopen = old_urlopen

def test_http_error(self):
error = requests.exceptions.HTTPError()
self.assertEqual(error.response, None)
response = requests.Response()
error = requests.exceptions.HTTPError(response=response)
self.assertEqual(error.response, response)
error = requests.exceptions.HTTPError('message', response=response)
self.assertEqual(str(error), 'message')
self.assertEqual(error.response, response)


if __name__ == '__main__':
unittest.main()

0 comments on commit c0d4b23

Please sign in to comment.