Skip to content

Commit 9035702

Browse files
committed
fix(pip/_internal): Add a generic network exception to be raised
1 parent 6a7bf94 commit 9035702

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

news/5380.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Raise user friendly errors on network failures

src/pip/_internal/cli/base_command.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
BadCommand,
2828
CommandError,
2929
InstallationError,
30+
NetworkConnectionError,
3031
PreviousBuildDirError,
3132
UninstallationError,
3233
)
@@ -195,7 +196,8 @@ def _main(self, args):
195196
logger.debug('Exception information:', exc_info=True)
196197

197198
return PREVIOUS_BUILD_DIR_ERROR
198-
except (InstallationError, UninstallationError, BadCommand) as exc:
199+
except (InstallationError, UninstallationError,
200+
BadCommand, NetworkConnectionError) as exc:
199201
logger.critical(str(exc))
200202
logger.debug('Exception information:', exc_info=True)
201203

src/pip/_internal/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ class PreviousBuildDirError(PipError):
8888
"""Raised when there's a previous conflicting build directory"""
8989

9090

91+
class NetworkConnectionError(PipError):
92+
"""HTTP connection error"""
93+
94+
9195
class InvalidWheelFilename(InstallationError):
9296
"""Invalid wheel filename."""
9397

src/pip/_internal/network/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from pip._vendor.requests.models import CONTENT_CHUNK_SIZE, Response
2+
from pip._vendor.urllib3.exceptions import NewConnectionError, ReadTimeoutError
23

4+
from pip._internal.exceptions import NetworkConnectionError
35
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
46

57
if MYPY_CHECK_RUNNING:
@@ -46,3 +48,8 @@ def response_chunks(response, chunk_size=CONTENT_CHUNK_SIZE):
4648
if not chunk:
4749
break
4850
yield chunk
51+
except (NewConnectionError, ReadTimeoutError) as exc:
52+
raise NetworkConnectionError(
53+
"Failed to get address for host! Check your network connectivity "
54+
"and DNS settings.\nDetails: {}".format(exc)
55+
)

0 commit comments

Comments
 (0)