Skip to content

Commit

Permalink
Pass in more structured context into exception
Browse files Browse the repository at this point in the history
This may help us provide better error messages if needed, instead of
being stuck with an eagerly-rendered string.
  • Loading branch information
uranusjr committed Aug 9, 2020
1 parent 2f6e82f commit e5ab371
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
39 changes: 23 additions & 16 deletions src/pip/_internal/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from typing import Any, Optional, List, Dict, Text

from pip._vendor.pkg_resources import Distribution
from pip._vendor.requests.models import Response, Request
from pip._vendor.six import PY3
from pip._vendor.six.moves import configparser

Expand Down Expand Up @@ -103,24 +102,32 @@ class PreviousBuildDirError(PipError):
class NetworkResponseError(PipError):
"""HTTP connection error"""

def __init__(self, error_msg, response=None, request=None):
# type: (Text, Response, Request) -> None
"""
Initialize NetworkResponseError with `request` and `response`
objects.
"""
self.response = response
self.request = request
self.error_msg = error_msg
if (self.response is not None and not self.request and
hasattr(response, 'request')):
self.request = self.response.request
super(NetworkResponseError, self).__init__(
error_msg, response, request)
error_type = "Unknown Error"

def __init__(self, status, reason, url):
# type: (int, Text, str) -> None
super(NetworkResponseError, self).__init__(status, reason, url)
self.status = status
self.reason = reason
self.url = url

def __str__(self):
# type: () -> str
return ensure_str(self.error_msg, errors="replace")
message = u"{} {}: {} for url: {}".format(
self.status,
self.error_type,
self.reason,
self.url,
)
return ensure_str(message, errors="replace")


class NetworkResponseClientError(NetworkResponseError):
error_type = "Client Error"


class NetworkResponseServerError(NetworkResponseError):
error_type = "Server Error"


class InvalidWheelFilename(InstallationError):
Expand Down
16 changes: 6 additions & 10 deletions src/pip/_internal/network/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from pip._vendor.requests.models import CONTENT_CHUNK_SIZE, Response

from pip._internal.exceptions import NetworkResponseError
from pip._internal.exceptions import (
NetworkResponseClientError,
NetworkResponseServerError,
)
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
Expand Down Expand Up @@ -30,7 +33,6 @@

def raise_for_status(resp):
# type: (Response) -> None
http_error_msg = u''
if isinstance(resp.reason, bytes):
# We attempt to decode utf-8 first because some servers
# choose to localize their reason strings. If the string
Expand All @@ -44,15 +46,9 @@ def raise_for_status(resp):
reason = resp.reason

if 400 <= resp.status_code < 500:
http_error_msg = u'%s Client Error: %s for url: %s' % (
resp.status_code, reason, resp.url)

raise NetworkResponseClientError(resp.status_code, reason, resp.url)
elif 500 <= resp.status_code < 600:
http_error_msg = u'%s Server Error: %s for url: %s' % (
resp.status_code, reason, resp.url)

if http_error_msg:
raise NetworkResponseError(http_error_msg, response=resp)
raise NetworkResponseServerError(resp.status_code, reason, resp.url)


def response_chunks(response, chunk_size=CONTENT_CHUNK_SIZE):
Expand Down

0 comments on commit e5ab371

Please sign in to comment.