harden lazy wheel wheel error handling · python-poetry/poetry@f2bfacb (original) (raw)
`@@ -41,15 +41,19 @@
`
41
41
`logger = logging.getLogger(name)
`
42
42
``
43
43
``
44
``
`-
class HTTPRangeRequestUnsupported(Exception):
`
``
44
`+
class LazyWheelUnsupportedError(Exception):
`
``
45
`+
"""Raised when a lazy wheel is unsupported."""
`
``
46
+
``
47
+
``
48
`+
class HTTPRangeRequestUnsupported(LazyWheelUnsupportedError):
`
45
49
`"""Raised when the remote server appears unable to support byte ranges."""
`
46
50
``
47
51
``
48
``
`-
class UnsupportedWheel(Exception):
`
``
52
`+
class UnsupportedWheel(LazyWheelUnsupportedError):
`
49
53
`"""Unsupported wheel."""
`
50
54
``
51
55
``
52
``
`-
class InvalidWheel(Exception):
`
``
56
`+
class InvalidWheel(LazyWheelUnsupportedError):
`
53
57
`"""Invalid (e.g. corrupt) wheel."""
`
54
58
``
55
59
`def init(self, location: str, name: str) -> None:
`
`@@ -85,6 +89,24 @@ def metadata_from_wheel_url(
`
85
89
`# themselves are invalid, not because we've messed up our bookkeeping
`
86
90
`# and produced an invalid file.
`
87
91
`raise InvalidWheel(url, name)
`
``
92
`+
except Exception as e:
`
``
93
`+
if isinstance(e, LazyWheelUnsupportedError):
`
``
94
`+
this is expected when the code handles issues with lazy wheel metadata retrieval correctly
`
``
95
`+
raise e
`
``
96
+
``
97
`+
logger.debug(
`
``
98
`+
"There was an unexpected %s when handling lazy wheel metadata retrieval for %s from %s: %s",
`
``
99
`+
type(e).name,
`
``
100
`+
name,
`
``
101
`+
url,
`
``
102
`+
e,
`
``
103
`+
)
`
``
104
+
``
105
`+
Catch all exception to handle any issues that may have occurred during
`
``
106
`+
attempts to use Lazy Wheel.
`
``
107
`+
raise LazyWheelUnsupportedError(
`
``
108
`+
f"Attempts to use lazy wheel metadata retrieval for {name} from {url} failed"
`
``
109
`+
) from e
`
88
110
``
89
111
``
90
112
`class MergeIntervals:
`
`@@ -590,6 +612,12 @@ def _try_initial_chunk_request(
`
590
612
`f"did not receive partial content: got code {code}"
`
591
613
` )
`
592
614
``
``
615
`+
if "Content-Range" not in tail.headers:
`
``
616
`+
raise LazyWheelUnsupportedError(
`
``
617
`+
f"file length cannot be determined for {self._url}, "
`
``
618
`+
f"did not receive content range header from server"
`
``
619
`+
)
`
``
620
+
593
621
`file_length = self._parse_full_length_from_content_range(
`
594
622
`tail.headers["Content-Range"]
`
595
623
` )
`
`@@ -614,10 +642,13 @@ def _extract_content_length(
`
614
642
``
615
643
`# This indicates that the requested range from the end was larger than the
`
616
644
`# actual file size: https://www.rfc-editor.org/rfc/rfc9110#status.416.
`
617
``
`-
if code == codes.requested_range_not_satisfiable:
`
``
645
`+
if (
`
``
646
`+
code == codes.requested_range_not_satisfiable
`
``
647
`+
and resp is not None
`
``
648
`+
and "Content-Range" in resp.headers
`
``
649
`+
):
`
618
650
`# In this case, we don't have any file content yet, but we do know the
`
619
651
`# size the file will be, so we can return that and exit here.
`
620
``
`-
assert resp is not None
`
621
652
`file_length = self._parse_full_length_from_content_range(
`
622
653
`resp.headers["Content-Range"]
`
623
654
` )
`