Skip to content

Commit ebb28a1

Browse files
committed
Better error message when link does not resolve
1 parent 312a081 commit ebb28a1

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

pystac/link.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import TYPE_CHECKING, Any, TypeVar
77

88
import pystac
9+
from pystac.errors import STACError
910
from pystac.html.jinja_env import get_jinja_env
1011
from pystac.utils import (
1112
HREF as HREF,
@@ -326,8 +327,12 @@ def resolve_stac_object(self, root: Catalog | None = None) -> Link:
326327
stac_io = owner_root._stac_io
327328
if stac_io is None:
328329
stac_io = pystac.StacIO.default()
329-
330-
obj = stac_io.read_stac_object(target_href, root=root)
330+
try:
331+
obj = stac_io.read_stac_object(target_href, root=root)
332+
except Exception as e:
333+
raise STACError(
334+
f"HREF: '{target_href}' does not resolve to a STAC object"
335+
) from e
331336
obj.set_self_href(target_href)
332337
if root is not None:
333338
obj = root._resolved_objects.get_or_cache(obj)

tests/test_catalog.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
Item,
2525
MediaType,
2626
)
27+
from pystac.errors import STACError
2728
from pystac.layout import (
2829
BestPracticesLayoutStrategy,
2930
HrefLayoutStrategy,
@@ -674,7 +675,7 @@ def test_save_unresolved(self) -> None:
674675
assert len(os.listdir(temporary_directory)) == 2
675676

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

680681
def test_generate_subcatalogs_works_with_custom_properties(self) -> None:

tests/test_link.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import pystac
1212
from pystac import Collection, Item, Link
13+
from pystac.errors import STACError
1314
from pystac.link import HIERARCHICAL_LINKS
1415
from pystac.utils import make_posix_style
1516
from tests.utils.test_cases import ARBITRARY_EXTENT
@@ -98,6 +99,13 @@ def test_resolve_stac_object_no_root_and_target_is_item(self) -> None:
9899
link = pystac.Link("my rel", target=self.item)
99100
link.resolve_stac_object()
100101

102+
def test_resolve_stac_object_throws_informative_error(self) -> None:
103+
link = pystac.Link("root", target="/a/b/foo.json")
104+
with pytest.raises(
105+
STACError, match="HREF: '/a/b/foo.json' does not resolve to a STAC object"
106+
):
107+
link.resolve_stac_object()
108+
101109
def test_resolved_self_href(self) -> None:
102110
catalog = pystac.Catalog(id="test", description="test desc")
103111
with TemporaryDirectory() as temporary_directory:

0 commit comments

Comments
 (0)