From 69d5086e8bd5bcc81e3877b05da5475dc1cc8ebe Mon Sep 17 00:00:00 2001 From: Pete Gadomski Date: Fri, 3 Jun 2022 11:37:39 -0600 Subject: [PATCH 1/2] feat: better error message in Client.open When trying to open a collection, we present a better exception / message. --- pystac_client/client.py | 20 ++++++++++++++++++++ pystac_client/errors.py | 4 ++++ tests/test_client.py | 6 ++++++ 3 files changed, 30 insertions(+) create mode 100644 pystac_client/errors.py diff --git a/pystac_client/client.py b/pystac_client/client.py index dd1c0dde..4e51a604 100644 --- a/pystac_client/client.py +++ b/pystac_client/client.py @@ -6,6 +6,7 @@ from pystac_client.collection_client import CollectionClient from pystac_client.conformance import ConformanceClasses +from pystac_client.errors import ClientTypeError from pystac_client.exceptions import APIError from pystac_client.item_search import ItemSearch from pystac_client.stac_api_io import StacApiIO @@ -93,6 +94,25 @@ def from_file( return cat + @classmethod + def from_dict( + cls, + d: Dict[str, Any], + href: Optional[str] = None, + root: Optional[pystac.Catalog] = None, + migrate: bool = False, + preserve_dict: bool = True, + ) -> "Client": + try: + return super().from_dict( + d=d, href=href, root=root, migrate=migrate, preserve_dict=preserve_dict + ) + except pystac.STACTypeError: + raise ClientTypeError( + f"Could not open Client (href={href}), " + f"expected type=Catalog, found type={d.get('type', None)}" + ) + @lru_cache() def get_collection(self, collection_id: str) -> CollectionClient: """Get a single collection from this Catalog/API diff --git a/pystac_client/errors.py b/pystac_client/errors.py new file mode 100644 index 00000000..ab5f573c --- /dev/null +++ b/pystac_client/errors.py @@ -0,0 +1,4 @@ +class ClientTypeError(Exception): + """Raised when trying to open a Client on a non-catalog STAC Object.""" + + pass diff --git a/tests/test_client.py b/tests/test_client.py index b06b0f6c..64172b35 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -11,6 +11,7 @@ from pystac_client import Client from pystac_client.conformance import ConformanceClasses +from pystac_client.errors import ClientTypeError from .helpers import STAC_URLS, TEST_DATA, read_data_file @@ -326,6 +327,11 @@ def test_get_collections_without_conformance(self, requests_mock): assert len(history) == 2 assert history[1].url == pc_collection_href + def test_opening_a_collection(self) -> None: + path = str(TEST_DATA / "planetary-computer-aster-l1t-collection.json") + with pytest.raises(ClientTypeError): + Client.open(path) + class TestAPISearch: @pytest.fixture(scope="function") From 7f4b7fbc57a4003581b7be6df1bdb777054e1971 Mon Sep 17 00:00:00 2001 From: Pete Gadomski Date: Fri, 3 Jun 2022 11:43:26 -0600 Subject: [PATCH 2/2] chore: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cc89a52..c481e386 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Search `filter-lang` defaults to `cql2-json` instead of `cql-json` [#169](https://github.com/stac-utils/pystac-client/pull/169) - Search `filter-lang` will be set to `cql2-json` if the `filter` is a dict, or `cql2-text` if it is a string [#169](https://github.com/stac-utils/pystac-client/pull/169) - Search parameter `intersects` is now typed to only accept a str, dict, or object that implements `__geo_interface__` [#169](https://github.com/stac-utils/pystac-client/pull/169) +- Better error message when trying to open a Collection with `Client.open` [#222](https://github.com/stac-utils/pystac-client/pull/222) ### Deprecated