From da122231e4dab3cc3f9726b4e8edf70bb649dd5d Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Fri, 29 Aug 2014 15:16:30 -0500 Subject: [PATCH 1/4] Capture and re-raise urllib3 ProtocolError --- requests/adapters.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/requests/adapters.py b/requests/adapters.py index 3c1e979f..6c6597da 100644 --- a/requests/adapters.py +++ b/requests/adapters.py @@ -23,6 +23,7 @@ from .packages.urllib3.exceptions import ConnectTimeoutError from .packages.urllib3.exceptions import HTTPError as _HTTPError from .packages.urllib3.exceptions import MaxRetryError from .packages.urllib3.exceptions import ProxyError as _ProxyError +from .packages.urllib3.exceptions import ProtocolError from .packages.urllib3.exceptions import ReadTimeoutError from .packages.urllib3.exceptions import SSLError as _SSLError from .cookies import extract_cookies_to_jar @@ -403,6 +404,9 @@ class HTTPAdapter(BaseAdapter): except socket.error as sockerr: raise ConnectionError(sockerr, request=request) + except ProtocolError as e: + raise ConnectionError(e, request=request) + except MaxRetryError as e: if isinstance(e.reason, ConnectTimeoutError): raise ConnectTimeout(e, request=request) From 2c76122a48c6a4b513930fed56c0aea5362d6c9c Mon Sep 17 00:00:00 2001 From: Kevin Burke Date: Fri, 29 Aug 2014 14:07:28 -0700 Subject: [PATCH 2/4] Add test that invalid domain raises a ConnectionError --- test_requests.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test_requests.py b/test_requests.py index 716c0dcf..3868f9dc 100755 --- a/test_requests.py +++ b/test_requests.py @@ -286,6 +286,11 @@ class RequestsTestCase(unittest.TestCase): r = s.get(url) assert r.status_code == 200 + def test_connection_error(self): + """Connecting to an unknown domain should raise a ConnectionError""" + with pytest.raises(ConnectionError): + requests.get("http://fooobarbangbazbing.httpbin.org") + def test_basicauth_with_netrc(self): auth = ('user', 'pass') wrong_auth = ('wronguser', 'wrongpass') From ce7957c085d912bd1b225460d159b19eaf3e6510 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Fri, 29 Aug 2014 16:50:45 -0500 Subject: [PATCH 3/4] Add test for invalid port --- test_requests.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test_requests.py b/test_requests.py index 3868f9dc..2ff61248 100755 --- a/test_requests.py +++ b/test_requests.py @@ -291,6 +291,9 @@ class RequestsTestCase(unittest.TestCase): with pytest.raises(ConnectionError): requests.get("http://fooobarbangbazbing.httpbin.org") + with pytest.raises(ConnectionError): + requests.get("http://httpbin.org:1") + def test_basicauth_with_netrc(self): auth = ('user', 'pass') wrong_auth = ('wronguser', 'wrongpass') From 9354855648cedbec3a91a9391e067f56e8414814 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Wed, 3 Sep 2014 16:01:25 -0500 Subject: [PATCH 4/4] Consolidate error handling --- requests/adapters.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/requests/adapters.py b/requests/adapters.py index 6c6597da..bf94bbe7 100644 --- a/requests/adapters.py +++ b/requests/adapters.py @@ -401,11 +401,8 @@ class HTTPAdapter(BaseAdapter): # All is well, return the connection to the pool. conn._put_conn(low_conn) - except socket.error as sockerr: - raise ConnectionError(sockerr, request=request) - - except ProtocolError as e: - raise ConnectionError(e, request=request) + except (ProtocolError, socket.error) as err: + raise ConnectionError(err, request=request) except MaxRetryError as e: if isinstance(e.reason, ConnectTimeoutError):