From 115b51e68c95d5aeea10c40719cf770303f4da61 Mon Sep 17 00:00:00 2001 From: Bryan Helmig Date: Wed, 4 Jan 2012 22:08:01 -0600 Subject: [PATCH 1/3] add eager mode for raising errors immediately --- AUTHORS.rst | 1 + requests/defaults.py | 2 ++ requests/models.py | 3 +++ test_requests.py | 10 ++++++++++ 4 files changed, 16 insertions(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index b9c3a643..e61f8d99 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -68,3 +68,4 @@ Patches and Suggestions - Christopher Davis - Ori Livneh - Jason Emerick +- Bryan Helmig diff --git a/requests/defaults.py b/requests/defaults.py index e8db67ab..8b3d6837 100644 --- a/requests/defaults.py +++ b/requests/defaults.py @@ -15,6 +15,7 @@ Configurations: :decode_unicode: Decode unicode responses automatically? :keep_alive: Reuse HTTP Connections? :max_retries: The number of times a request should be retried in the event of a connection failure. +:eager_mode: If true, Requests will raise errors immediately. :safe_mode: If true, Requests will catch all errors. :pool_maxsize: The maximium size of an HTTP connection pool. :pool_connections: The number of active HTTP connection pools to use. @@ -38,5 +39,6 @@ defaults['decode_unicode'] = True defaults['pool_connections'] = 10 defaults['pool_maxsize'] = 10 defaults['max_retries'] = 0 +defaults['eager_mode'] = False defaults['safe_mode'] = False defaults['keep_alive'] = True diff --git a/requests/models.py b/requests/models.py index 17403a69..f59019c8 100644 --- a/requests/models.py +++ b/requests/models.py @@ -525,6 +525,9 @@ class Request(object): if prefetch: # Save the response. self.response.content + + if self.config.get('eager_mode'): + self.response.raise_for_status() return self.sent diff --git a/test_requests.py b/test_requests.py index b3e55c8c..c388deaf 100755 --- a/test_requests.py +++ b/test_requests.py @@ -279,6 +279,16 @@ class RequestsTestSuite(unittest.TestCase): r.raise_for_status() + def test_default_status_raising(self): + config = {'eager_mode': True} + args = [httpbin('status', '404')] + kwargs = dict(config=config) + self.assertRaises(HTTPError, requests.get, *args, **kwargs) + + r = requests.get(httpbin('status', '200')) + self.assertEqual(r.status_code, 200) + + def test_decompress_gzip(self): r = requests.get(httpbin('gzip')) From 459a91c54fc42ef385ca24811ea0833f38fd433b Mon Sep 17 00:00:00 2001 From: Bryan Helmig Date: Thu, 5 Jan 2012 12:50:44 -0600 Subject: [PATCH 2/3] highway to the danger_mode --- requests/defaults.py | 4 ++-- requests/models.py | 2 +- test_requests.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/requests/defaults.py b/requests/defaults.py index 8b3d6837..1d6716df 100644 --- a/requests/defaults.py +++ b/requests/defaults.py @@ -15,7 +15,7 @@ Configurations: :decode_unicode: Decode unicode responses automatically? :keep_alive: Reuse HTTP Connections? :max_retries: The number of times a request should be retried in the event of a connection failure. -:eager_mode: If true, Requests will raise errors immediately. +:danger_mode: If true, Requests will raise errors immediately. :safe_mode: If true, Requests will catch all errors. :pool_maxsize: The maximium size of an HTTP connection pool. :pool_connections: The number of active HTTP connection pools to use. @@ -39,6 +39,6 @@ defaults['decode_unicode'] = True defaults['pool_connections'] = 10 defaults['pool_maxsize'] = 10 defaults['max_retries'] = 0 -defaults['eager_mode'] = False +defaults['danger_mode'] = False defaults['safe_mode'] = False defaults['keep_alive'] = True diff --git a/requests/models.py b/requests/models.py index f59019c8..39ea7a11 100644 --- a/requests/models.py +++ b/requests/models.py @@ -526,7 +526,7 @@ class Request(object): # Save the response. self.response.content - if self.config.get('eager_mode'): + if self.config.get('danger_mode'): self.response.raise_for_status() return self.sent diff --git a/test_requests.py b/test_requests.py index c388deaf..f6547de0 100755 --- a/test_requests.py +++ b/test_requests.py @@ -280,7 +280,7 @@ class RequestsTestSuite(unittest.TestCase): def test_default_status_raising(self): - config = {'eager_mode': True} + config = {'danger_mode': True} args = [httpbin('status', '404')] kwargs = dict(config=config) self.assertRaises(HTTPError, requests.get, *args, **kwargs) From aa39fb0c80f3b5aeb82785288bf351ddae7c4ed5 Mon Sep 17 00:00:00 2001 From: Bryan Helmig Date: Thu, 5 Jan 2012 14:11:19 -0600 Subject: [PATCH 3/3] mention of error modes in quickstart --- docs/user/quickstart.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index 34741174..551d0c34 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -364,6 +364,10 @@ If a request exceeds the configured number of maximum redirections, a :class:`To All exceptions that Requests explicitly raises inherit from :class:`requests.exceptions.RequestException`. +You can refer to :ref:`Configuration API Docs ` for immediate raising of :class:`HTTPError` exceptions +via the ``danger_mode`` option or have Requests catch the majority of :class:`requests.exceptions.RequestException` exceptions +with the ``safe_mode`` option. + ----------------------- Ready for more? Check out the :ref:`advanced ` section.