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/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. diff --git a/requests/defaults.py b/requests/defaults.py index e8db67ab..1d6716df 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. +: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. @@ -38,5 +39,6 @@ defaults['decode_unicode'] = True defaults['pool_connections'] = 10 defaults['pool_maxsize'] = 10 defaults['max_retries'] = 0 +defaults['danger_mode'] = False defaults['safe_mode'] = False defaults['keep_alive'] = True diff --git a/requests/models.py b/requests/models.py index 17403a69..39ea7a11 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('danger_mode'): + self.response.raise_for_status() return self.sent diff --git a/test_requests.py b/test_requests.py index b3e55c8c..f6547de0 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 = {'danger_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'))