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

Better error message when link does not resolve #1500

Merged
merged 4 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 7 additions & 2 deletions pystac/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import TYPE_CHECKING, Any, TypeVar

import pystac
from pystac.errors import STACError
from pystac.html.jinja_env import get_jinja_env
from pystac.utils import (
HREF as HREF,
Expand Down Expand Up @@ -326,8 +327,12 @@ def resolve_stac_object(self, root: Catalog | None = None) -> Link:
stac_io = owner_root._stac_io
if stac_io is None:
stac_io = pystac.StacIO.default()

obj = stac_io.read_stac_object(target_href, root=root)
try:
obj = stac_io.read_stac_object(target_href, root=root)
except Exception as e:
raise STACError(
f"HREF: '{target_href}' does not resolve to a STAC object"
) from e
obj.set_self_href(target_href)
if root is not None:
obj = root._resolved_objects.get_or_cache(obj)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Item,
MediaType,
)
from pystac.errors import STACError
from pystac.layout import (
BestPracticesLayoutStrategy,
HrefLayoutStrategy,
Expand Down Expand Up @@ -674,7 +675,7 @@ def test_save_unresolved(self) -> None:
assert len(os.listdir(temporary_directory)) == 2

with tempfile.TemporaryDirectory() as temporary_directory:
with pytest.raises(FileNotFoundError):
with pytest.raises(STACError, match="does not resolve to a STAC object"):
catalog.normalize_and_save(temporary_directory, skip_unresolved=False)

def test_generate_subcatalogs_works_with_custom_properties(self) -> None:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import pystac
from pystac import Collection, Item, Link
from pystac.errors import STACError
from pystac.link import HIERARCHICAL_LINKS
from pystac.utils import make_posix_style
from tests.utils.test_cases import ARBITRARY_EXTENT
Expand Down Expand Up @@ -98,6 +99,14 @@ def test_resolve_stac_object_no_root_and_target_is_item(self) -> None:
link = pystac.Link("my rel", target=self.item)
link.resolve_stac_object()

@pytest.mark.skipif(os.name == "nt", reason="Non-windows test")
def test_resolve_stac_object_throws_informative_error(self) -> None:
link = pystac.Link("root", target="/a/b/foo.json")
with pytest.raises(
STACError, match="HREF: '/a/b/foo.json' does not resolve to a STAC object"
):
link.resolve_stac_object()

def test_resolved_self_href(self) -> None:
catalog = pystac.Catalog(id="test", description="test desc")
with TemporaryDirectory() as temporary_directory:
Expand Down
Loading