Unwrap ProxyError when evaluating retries
MODIFY
src/urllib3/exceptions.py
src/urllib3/exceptions.py
@@ -45,7 +45,10 @@ class SSLError(HTTPError):
class ProxyError(HTTPError):
"Raised when the connection to a proxy fails."
- pass
+
+ def __init__(self, message, error, *args):
+ super(ProxyError, self).__init__(message, error, *args)
+ self.original_error = error
class DecodeError(HTTPError):

MODIFY
src/urllib3/util/retry.py
src/urllib3/util/retry.py
@@ -13,6 +13,7 @@ from ..exceptions import (
ReadTimeoutError,
ResponseError,
InvalidHeader,
+ ProxyError,
)
from ..packages import six
@@ -306,6 +307,8 @@ class Retry(object):
""" Errors when we're fairly sure that the server did not receive the
request, so it should be safe to retry.
"""
+ if isinstance(err, ProxyError):
+ err = err.original_error
return isinstance(err, ConnectTimeoutError)
def _is_read_error(self, err):

MODIFY
test/test_proxymanager.py
test/test_proxymanager.py
import pytest
+from .port_helpers import find_unused_port
from urllib3.poolmanager import ProxyManager
from urllib3.util.url import parse_url
+from urllib3.util.retry import Retry
+from urllib3.exceptions import (
+ MaxRetryError,
+ ProxyError,
+ NewConnectionError,
+)
class TestProxyManager(object):
@@ -57,3 +64,17 @@ class TestProxyManager(object):
with ProxyManager("https://proxy:8080") as p:
assert p._proxy_requires_url_absolute_form(http_url)
assert p._proxy_requires_url_absolute_form(https_url)
+
+ def test_proxy_connect_retry(self):
+ retry = Retry(total=None, connect=False)
+ with find_unused_port() as port:
+ with ProxyManager("http://localhost:{}".format(port)) as p:
+ with pytest.raises(ProxyError) as ei:
+ p.urlopen("HEAD", url="http://localhost/", retries=retry)
+ assert isinstance(ei.value.original_error, NewConnectionError)
+
+ retry = Retry(total=None, connect=2)
+ with ProxyManager("http://localhost:{}".format(port)) as p:
+ with pytest.raises(MaxRetryError) as ei:
+ p.urlopen("HEAD", url="http://localhost/", retries=retry)
+ assert isinstance(ei.value.reason.original_error, NewConnectionError)

Unwrap ProxyError when evaluating retries (#1830)