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

feat(partitions): custom error for nonexistent partition #826

Merged
merged 8 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
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
24 changes: 19 additions & 5 deletions craft_parts/dirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from pathlib import Path
from types import MappingProxyType

from craft_parts.errors import PartitionNotFound, PartitionUsageError
from craft_parts.utils import partition_utils


Expand Down Expand Up @@ -74,14 +75,27 @@ def __init__(
)
)

def _validate_requested_partition(
self, dir_name: str, partition: str | None = None
) -> None:
"""Ensure the requested partition is valid."""
if self._partitions:
if not partition:
raise PartitionUsageError(
error_list=[
f"Partitions are enabled, you must specify which partition's {dir_name!r} you want."
],
partitions=self._partitions,
)
if partition not in self._partitions:
raise PartitionNotFound(partition, self._partitions)

def get_stage_dir(self, partition: str | None = None) -> Path:
"""Get the stage directory for the given partition."""
if self._partitions and partition not in self._partitions:
raise ValueError(f"Unknown partition {partition}")
self._validate_requested_partition("stage_dir", partition)
return self.stage_dirs[partition]

def get_prime_dir(self, partition: str | None = None) -> Path:
"""Get the stage directory for the given partition."""
if self._partitions and partition not in self._partitions:
raise ValueError(f"Unknown partition {partition}")
"""Get the prime directory for the given partition."""
self._validate_requested_partition("prime_dir", partition)
return self.prime_dirs[partition]
26 changes: 24 additions & 2 deletions craft_parts/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,17 +652,22 @@ class PartitionUsageError(PartitionError):
"""Error for a list of invalid partition usages.

:param error_list: Iterable of strings describing the invalid usages.
:param partitions: Iterable of the names of valid partitions.
:param brief: Override brief message.
"""

def __init__(
self, error_list: Iterable[str], partitions: Iterable[str] | None
self,
error_list: Iterable[str],
partitions: Iterable[str] | None,
brief: str | None = None,
) -> None:
valid_partitions = (
f"\nValid partitions: {', '.join(partitions)}" if partitions else ""
)

super().__init__(
brief="Invalid usage of partitions",
brief=brief or "Invalid usage of partitions",
details="\n".join(error_list) + valid_partitions,
resolution="Correct the invalid partition name(s) and try again.",
)
Expand All @@ -688,3 +693,20 @@ def __init__(self, warning_list: Iterable[str]) -> None:
),
)
Warning.__init__(self)


class PartitionNotFound(PartitionUsageError):
"""A partition has been specified that does not exist.

:param partition_name: The name of the partition that does not exist.
:param partitions: Iterable of the names of valid partitions.
"""

def __init__(self, partition_name: str, partitions: Iterable[str]) -> None:
# Allow callers catching this exception easy access to the partition name
self.partition_name = partition_name
mattculler marked this conversation as resolved.
Show resolved Hide resolved
super().__init__(
brief=f"Requested partition does not exist: {partition_name!r}",
partitions=partitions,
error_list=[],
)
1 change: 1 addition & 0 deletions docs/common/craft-parts/craft-parts.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ PartInfo
PartSpec
PartSpecificationError
PartitionError
PartitionNotFound
PartitionPathPair
PartitionUsageError
PartitionUsageWarning
Expand Down
Loading