From d36b467878f4a7c74337b6134680fa8dff6d1def Mon Sep 17 00:00:00 2001 From: taran_lu Date: Mon, 22 May 2023 16:22:06 -0700 Subject: [PATCH] Add try/catch to catch ConnectionErrors that can occur when using requests to get package url - Specificall, this resolves pdm-project/pdm#1945 which was caused by an unknown host name due to not having a DNS entry. --- src/unearth/collector.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/unearth/collector.py b/src/unearth/collector.py index 4c035c0..58e06d1 100644 --- a/src/unearth/collector.py +++ b/src/unearth/collector.py @@ -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 @@ -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