Skip to content

Commit

Permalink
Implement custom exceptions for specific error situations (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhfrank authored Jul 2, 2024
1 parent 7c869ce commit 6c9b2ba
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
7 changes: 5 additions & 2 deletions articat/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from articat.artifact import ID, Artifact, Metadata, Partition, Version, not_supplied
from articat.config import ArticatConfig, ConfigMixin
from articat.exceptions import MissingArtifactException

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -114,7 +115,9 @@ def get( # type: ignore[no-untyped-def]
)
except StopIteration as e:
req = {"id": id, "partition": partition, "version": version, "dev": dev}
raise ValueError(f"Can't find requested artifact {req}") from e
raise MissingArtifactException(
f"Can't find requested artifact {req}"
) from e

@classmethod
@overload
Expand Down Expand Up @@ -142,7 +145,7 @@ def latest_partition( # type: ignore[no-untyped-def]
)
)
except StopIteration as e:
raise ValueError(f"Can't find requested artifact {id}") from e
raise MissingArtifactException(f"Can't find requested artifact {id}") from e

@classmethod
@overload
Expand Down
4 changes: 4 additions & 0 deletions articat/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class MissingArtifactException(ValueError):
"""Exception raised when an artifact cannot be found."""

pass
5 changes: 3 additions & 2 deletions articat/tests/catalog_datastore_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from dateutil.tz import UTC

from articat.artifact import EXECUTION_URL_ENV_NAME, ID, Metadata
from articat.exceptions import MissingArtifactException
from articat.fs_artifact import FSArtifact
from articat.tests.utils import (
TestCatalog,
Expand Down Expand Up @@ -234,12 +235,12 @@ def test_catalog_latest_partition(uid: ID) -> None:


def test_catalog_get_nice_error_on_missing_get(uid: ID) -> None:
with pytest.raises(ValueError, match="Can't find requested artifact"):
with pytest.raises(MissingArtifactException, match="Can't find requested artifact"):
TestCatalog.get(uid, version="0.1.1")


def test_catalog_get_nice_error_on_missing_latest(uid: ID) -> None:
with pytest.raises(ValueError, match="Can't find requested artifact"):
with pytest.raises(MissingArtifactException, match="Can't find requested artifact"):
TestCatalog.latest_partition(uid)


Expand Down
13 changes: 13 additions & 0 deletions articat/tests/catalog_local_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest

from articat.catalog_local import CatalogLocal
from articat.exceptions import MissingArtifactException


def test_catalog_local_missing_artifact_exception():
"""
Test that `MissingArtifactException` is raised when an artifact cannot be found in `CatalogLocal`.
"""
catalog = CatalogLocal()
with pytest.raises(MissingArtifactException, match="Can't find requested artifact"):
catalog.get("non_existing_artifact_id")
1 change: 0 additions & 1 deletion articat/tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from articat.utils.typing import PathType
from articat.utils.utils import download_artifact, dummy_unsafe_cache, get_repo_and_hash


def get_source_path_that_looks_like_path_from_catalog() -> Path:
p = Path(
tempfile.mkdtemp(dir=TestFSArtifact.config().fs_prod_prefix(), suffix="__ID__")
Expand Down

0 comments on commit 6c9b2ba

Please sign in to comment.