Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Add test coverage for stac_api_io.py error handling ([#835](https://github.com/stac-utils/pystac-client/pull/835))

### Changed

- Make `get_collection` raise if `collection_id` is empty ([#809](https://github.com/stac-utils/pystac-client/pull/809))
Expand Down
6 changes: 5 additions & 1 deletion pystac_client/stac_api_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,11 @@ def stac_object_from_dict(
d, href=str(href), root=root, migrate=False, preserve_dict=preserve_dict
)

raise ValueError(f"Unknown STAC object type {info.object_type}")
raise ValueError(
f"Unknown STAC object type {info.object_type}"
) # pragma: no cover
# This is unreachable because pystac.identify_stac_object raises STACTypeError
# for unknown object types before this code is executed

def get_pages(
self,
Expand Down
24 changes: 24 additions & 0 deletions tests/test_stac_api_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,27 @@ def test_stac_io_in_pystac() -> None:
stac_io = root._stac_io
assert isinstance(stac_io, StacApiIO)
assert stac_io.timeout == 42


def test_request_decode_error(requests_mock: Mocker) -> None:
"""Test that decode errors in request() are properly handled."""
url = "https://example.com/bad-encoding"
# Mock a response with invalid UTF-8 content
requests_mock.get(url, status_code=200, content=b"\xff\xfe\x00\x00")

stac_api_io = StacApiIO()

with pytest.raises(APIError) as excinfo:
stac_api_io.request(url)

assert (
"decode" in str(excinfo.value).lower() or "utf-8" in str(excinfo.value).lower()
)


def test_write_text_to_href_url_error() -> None:
"""Test that write_text_to_href raises APIError for URLs."""
stac_api_io = StacApiIO()

with pytest.raises(APIError, match="Transactions not supported"):
stac_api_io.write_text_to_href("https://example.com/write", "content")