Skip to content

Commit 67931b5

Browse files
authored
Better error message when link does not resolve (#1500)
* Better error message when link does not resolve * Skip test on windows * Update changelog
1 parent bc5eae4 commit 67931b5

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- Update Projection Extension to version 2 - proj:epsg -> proj:code ([#1287](https://github.com/stac-utils/pystac/pull/1287))
1717
- Update migrate code to handle license changes in STAC spec 1.1.0 ([#1491](https://github.com/stac-utils/pystac/pull/1491))
1818
- Allow links to have `file://` prefix - but don't write them that way by default ([#1489](https://github.com/stac-utils/pystac/pull/1489))
19+
- Raise `STACError` with message when a link is expected to resolve to a STAC object but doesn't ([#1500](https://github.com/stac-utils/pystac/pull/1500))
1920

2021
### Fixed
2122

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,
@@ -327,8 +328,12 @@ def resolve_stac_object(self, root: Catalog | None = None) -> Link:
327328
stac_io = owner_root._stac_io
328329
if stac_io is None:
329330
stac_io = pystac.StacIO.default()
330-
331-
obj = stac_io.read_stac_object(target_href, root=root)
331+
try:
332+
obj = stac_io.read_stac_object(target_href, root=root)
333+
except Exception as e:
334+
raise STACError(
335+
f"HREF: '{target_href}' does not resolve to a STAC object"
336+
) from e
332337
obj.set_self_href(target_href)
333338
if root is not None:
334339
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: 9 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,14 @@ 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+
@pytest.mark.skipif(os.name == "nt", reason="Non-windows test")
103+
def test_resolve_stac_object_throws_informative_error(self) -> None:
104+
link = pystac.Link("root", target="/a/b/foo.json")
105+
with pytest.raises(
106+
STACError, match="HREF: '/a/b/foo.json' does not resolve to a STAC object"
107+
):
108+
link.resolve_stac_object()
109+
101110
def test_resolved_self_href(self) -> None:
102111
catalog = pystac.Catalog(id="test", description="test desc")
103112
with TemporaryDirectory() as temporary_directory:

0 commit comments

Comments
 (0)