diff --git a/CHANGELOG.md b/CHANGELOG.md index 742ac45c..a756af72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/pystac_client/stac_api_io.py b/pystac_client/stac_api_io.py index 0daf73e5..479c9c99 100644 --- a/pystac_client/stac_api_io.py +++ b/pystac_client/stac_api_io.py @@ -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, diff --git a/tests/test_stac_api_io.py b/tests/test_stac_api_io.py index 46369a26..948601e1 100644 --- a/tests/test_stac_api_io.py +++ b/tests/test_stac_api_io.py @@ -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")