Skip to content

Commit 1d0548b

Browse files
agriyakhetarpalryanking13pre-commit-ci[bot]
authored
Don't raise HTTPStatusError on CORS errors when querying packages in a transaction (#225)
* Don't raise HTTPStatusError on CORS errors * Better log message Co-authored-by: Gyeongjae Choi <def6488@gmail.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Drop `micropip._compat.HttpStatusError` * Drop HttpStatusError completely --------- Co-authored-by: Gyeongjae Choi <def6488@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 61877cb commit 1d0548b

6 files changed

Lines changed: 12 additions & 78 deletions

File tree

micropip/_compat/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434

3535
to_js = compatibility_layer.to_js
3636

37-
HttpStatusError = compatibility_layer.HttpStatusError
38-
39-
4037
__all__ = [
4138
"LOCKFILE_INFO",
4239
"LOCKFILE_PACKAGES",
@@ -47,5 +44,4 @@
4744
"loadPackage",
4845
"get_dynlibs",
4946
"to_js",
50-
"HttpStatusError",
5147
]

micropip/_compat/_compat_in_pyodide.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from pyodide._package_loader import get_dynlibs
99
from pyodide.ffi import IN_BROWSER, to_js
10-
from pyodide.http import HttpStatusError, pyfetch
10+
from pyodide.http import pyfetch
1111

1212
from .compatibility_layer import CompatibilityLayer
1313

@@ -28,14 +28,6 @@
2828

2929

3030
class CompatibilityInPyodide(CompatibilityLayer):
31-
class HttpStatusError(Exception):
32-
status_code: int
33-
message: str
34-
35-
def __init__(self, status_code: int, message: str):
36-
self.status_code = status_code
37-
self.message = message
38-
super().__init__(message)
3931

4032
@staticmethod
4133
async def fetch_bytes(url: str, kwargs: dict[str, str]) -> bytes:
@@ -51,11 +43,9 @@ async def fetch_bytes(url: str, kwargs: dict[str, str]) -> bytes:
5143
async def fetch_string_and_headers(
5244
url: str, kwargs: dict[str, str]
5345
) -> tuple[str, dict[str, str]]:
54-
try:
55-
response = await pyfetch(url, **kwargs)
56-
response.raise_for_status()
57-
except HttpStatusError as e:
58-
raise CompatibilityInPyodide.HttpStatusError(e.status, str(e)) from e
46+
47+
response = await pyfetch(url, **kwargs)
48+
response.raise_for_status()
5949

6050
content = await response.string()
6151
headers: dict[str, str] = response.headers

micropip/_compat/_compat_not_in_pyodide.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import re
22
from pathlib import Path
33
from typing import IO, TYPE_CHECKING, Any
4-
from urllib.error import HTTPError
54
from urllib.request import Request, urlopen
65
from urllib.response import addinfourl
76

@@ -17,15 +16,6 @@ class CompatibilityNotInPyodide(CompatibilityLayer):
1716
# TODO: use packaging APIs here instead?
1817
_canonicalize_regex = re.compile(r"[-_.]+")
1918

20-
class HttpStatusError(Exception):
21-
status_code: int
22-
message: str
23-
24-
def __init__(self, status_code: int, message: str):
25-
self.status_code = status_code
26-
self.message = message
27-
super().__init__(message)
28-
2919
class loadedPackages(CompatibilityLayer.loadedPackages):
3020
@staticmethod
3121
def to_py():
@@ -43,11 +33,7 @@ async def fetch_bytes(url: str, kwargs: dict[str, Any]) -> bytes:
4333
async def fetch_string_and_headers(
4434
url: str, kwargs: dict[str, Any]
4535
) -> tuple[str, dict[str, str]]:
46-
try:
47-
response = CompatibilityNotInPyodide._fetch(url, kwargs=kwargs)
48-
except HTTPError as e:
49-
raise CompatibilityNotInPyodide.HttpStatusError(e.code, str(e)) from e
50-
36+
response = CompatibilityNotInPyodide._fetch(url, kwargs=kwargs)
5137
headers = {k.lower(): v for k, v in response.headers.items()}
5238
return response.read().decode(), headers
5339

micropip/_compat/compatibility_layer.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ class CompatibilityLayer(ABC):
1313
All of the following methods / properties must be implemented for use both inside and outside of pyodide.
1414
"""
1515

16-
class HttpStatusError(ABC, Exception):
17-
status_code: int
18-
message: str
19-
20-
@abstractmethod
21-
def __init__(self, status_code: int, message: str):
22-
pass
23-
2416
class loadedPackages(ABC):
2517
@staticmethod
2618
@abstractmethod

micropip/package_index.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import Any
1010
from urllib.parse import urljoin, urlparse, urlunparse
1111

12-
from ._compat import HttpStatusError, fetch_string_and_headers
12+
from ._compat import fetch_string_and_headers
1313
from ._utils import is_package_compatible, parse_version
1414
from ._vendored.mousebender.simple import from_project_details_html
1515
from ._vendored.packaging.src.packaging.utils import InvalidWheelFilename
@@ -313,14 +313,14 @@ async def query_package(
313313
logger.debug("Url has no placeholder, appending package name : %r", url)
314314
try:
315315
metadata, headers = await fetch_string_and_headers(url, _fetch_kwargs)
316-
except HttpStatusError as e:
317-
if e.status_code == 404:
318-
logger.debug("NotFound (404) for %r, trying next index.", url)
319-
continue
316+
except Exception as e:
320317
logger.debug(
321-
"Error fetching %r (%s), trying next index.", url, e.status_code
318+
"Error fetching metadata for the package %r from (%r): %r, trying next index.",
319+
name,
320+
url,
321+
e,
322322
)
323-
raise
323+
continue
324324

325325
content_type = headers.get("content-type", "").lower()
326326
try:

tests/test_compat.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)