Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add try/catch to catch ConnectionErrors #54

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions src/unearth/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Iterable, NamedTuple
from urllib import parse

from requests.exceptions import ConnectionError
from requests.models import Response

from unearth.link import Link
Expand Down Expand Up @@ -172,21 +173,25 @@ def _get_html_response(session: PyPISession, location: Link) -> Response:
# the link is an HTML page to avoid downloading a large file.
_ensure_index_response(session, location)

resp = session.get(
location.normalized,
headers={
"Accept": ", ".join(
[
"application/vnd.pypi.simple.v1+json",
"application/vnd.pypi.simple.v1+html; q=0.1",
"text/html; q=0.01",
]
),
# Don't cache the /simple/{package} page, to ensure it gets updated
# immediately when a new release is uploaded.
"Cache-Control": "max-age=0",
},
)
try:
resp = session.get(
location.normalized,
headers={
"Accept": ", ".join(
[
"application/vnd.pypi.simple.v1+json",
"application/vnd.pypi.simple.v1+html; q=0.1",
"text/html; q=0.01",
]
),
# Don't cache the /simple/{package} page, to ensure it gets updated
# immediately when a new release is uploaded.
"Cache-Control": "max-age=0",
},
)
except ConnectionError as e:
raise LinkCollectError(f"Connection Error: {e}")

_check_for_status(resp)
_ensure_index_content_type(resp)
return resp
Expand Down