Skip to content

Commit

Permalink
Move errors to a separate file.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Oct 21, 2024
1 parent 86f40d6 commit cfb7483
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
24 changes: 24 additions & 0 deletions micropip/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
A module to define micropip custom Exceptions.
"""


class UnsupportedContentTypeError(Exception):
"""raise when selecting a parser for current index
This is raise if the current content type is not recognized.
"""


class NoCompatibleWheelError(Exception):
"""
This is raised when a package is found but have no wheel compatible with
current pyodide.
"""


class PackageNotFoundOnAnyIndexError(Exception):
"""
This is raised if current package was not found on any of the currently
listed index.
"""
11 changes: 2 additions & 9 deletions micropip/package_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from ._compat import HttpStatusError, fetch_string_and_headers
from ._utils import is_package_compatible, parse_version
from .errors import PackageNotFoundOnAnyIndexError, UnsupportedContentTypeError
from .externals.mousebender.simple import from_project_details_html
from .wheelinfo import WheelInfo

Expand Down Expand Up @@ -218,10 +219,6 @@ def _contain_placeholder(url: str, placeholder: str = "package_name") -> bool:
return placeholder in fields


class UnsupportedContentTypeError(Exception):
pass


def _select_parser(content_type: str, pkgname: str) -> Callable[[str], ProjectInfo]:
"""
Select the function to parse the response based on the content type.
Expand All @@ -243,10 +240,6 @@ def _select_parser(content_type: str, pkgname: str) -> Callable[[str], ProjectIn
)


class NoValidIndexForPackageError(Exception):
pass


async def query_package(
name: str,
fetch_kwargs: dict[str, Any] | None = None,
Expand Down Expand Up @@ -309,7 +302,7 @@ async def query_package(
parser = _select_parser(content_type, name)
return parser(metadata)
else:
raise NoValidIndexForPackageError(
raise PackageNotFoundOnAnyIndexError(
f"Can't fetch metadata for {name!r}. "
"Please make sure you have entered a correct package name "
"and correctly specified index_urls (if you changed them)."
Expand Down
11 changes: 4 additions & 7 deletions micropip/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
from ._compat import REPODATA_PACKAGES
from ._utils import best_compatible_tag_index, check_compatible
from .constants import FAQ_URLS
from .errors import NoCompatibleWheelError
from .package import PackageMetadata
from .package_index import NoValidIndexForPackageError, ProjectInfo
from .package_index import PackageNotFoundOnAnyIndexError, ProjectInfo
from .wheelinfo import WheelInfo

logger = logging.getLogger("micropip")
Expand Down Expand Up @@ -153,7 +154,7 @@ def eval_marker(e: dict[str, str]) -> bool:
else:
try:
await self._add_requirement_from_package_index(req)
except NoValidIndexForPackageError:
except PackageNotFoundOnAnyIndexError:
logger.warning(
"Transaction: package %r was not found in any index, "
"falling back to pyodide lock file",
Expand All @@ -178,7 +179,7 @@ def eval_marker(e: dict[str, str]) -> bool:
)

raise
except (NoCompatibleWheelError, NoValidIndexForPackageError):
except (NoCompatibleWheelError, PackageNotFoundOnAnyIndexError):
self.failed.append(req)
if not self.keep_going:
raise
Expand Down Expand Up @@ -263,10 +264,6 @@ async def add_wheel(
self.wheels.append(wheel)


class NoCompatibleWheelError(Exception):
pass


def find_wheel(metadata: ProjectInfo, req: Requirement) -> WheelInfo:
"""Parse metadata to find the latest version of pure python wheel.
Parameters
Expand Down

0 comments on commit cfb7483

Please sign in to comment.