Skip to content

Commit

Permalink
Display better messages on legacy repo HTTP errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-hipschman authored and finswimmer committed Aug 15, 2020
1 parent 5dd5411 commit 3f7545d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
11 changes: 8 additions & 3 deletions poetry/repositories/legacy_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from ..inspection.info import PackageInfo
from .auth import Auth
from .exceptions import PackageNotFound
from .exceptions import RepositoryError
from .pypi_repository import PyPiRepository


Expand Down Expand Up @@ -361,8 +362,12 @@ def _get_release_info(self, name, version): # type: (str, str) -> dict

def _get(self, endpoint): # type: (str) -> Union[Page, None]
url = self._url + endpoint
response = self.session.get(url)
if response.status_code == 404:
return
try:
response = self.session.get(url)
if response.status_code == 404:
return
response.raise_for_status()
except requests.HTTPError as e:
raise RepositoryError(e)

return Page(url, response.content, response.headers)
34 changes: 34 additions & 0 deletions tests/repositories/test_legacy_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from poetry.core.packages import Dependency
from poetry.repositories.auth import Auth
from poetry.repositories.exceptions import PackageNotFound
from poetry.repositories.exceptions import RepositoryError
from poetry.repositories.legacy_repository import LegacyRepository
from poetry.repositories.legacy_repository import Page
from poetry.utils._compat import PY35
Expand Down Expand Up @@ -278,3 +279,36 @@ def test_username_password_special_chars():
repo = MockRepository(auth=auth)

assert "http://user%3A:%2F%252Fp%40ssword@legacy.foo.bar" == repo.authenticated_url


class MockHttpRepository(LegacyRepository):
def __init__(self, endpoint_responses, http):
base_url = "http://legacy.foo.bar"
super(MockHttpRepository, self).__init__(
"legacy", url=base_url, auth=None, disable_cache=True
)

for endpoint, response in endpoint_responses.items():
url = base_url + endpoint
http.register_uri(http.GET, url, status=response)


def test_get_200_returns_page(http):
repo = MockHttpRepository({"/foo": 200}, http)

assert repo._get("/foo")


def test_get_404_returns_none(http):
repo = MockHttpRepository({"/foo": 404}, http)

assert repo._get("/foo") is None


def test_get_4xx_and_5xx_raises(http):
endpoints = {"/{}".format(code): code for code in {401, 403, 500}}
repo = MockHttpRepository(endpoints, http)

for endpoint in endpoints:
with pytest.raises(RepositoryError):
repo._get(endpoint)

0 comments on commit 3f7545d

Please sign in to comment.