Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling when accidentally importing a Collection with Catalog #186

Closed
CloudNiner opened this issue Sep 11, 2020 · 3 comments · Fixed by #221
Closed

Improve error handling when accidentally importing a Collection with Catalog #186

CloudNiner opened this issue Sep 11, 2020 · 3 comments · Fixed by #221
Labels
bug Things which are broken

Comments

@CloudNiner
Copy link
Contributor

CloudNiner commented Sep 11, 2020

Today I accidentally used pystac.Catalog.from_file to read a collection.json instead of pystac.Collection.from_file and got this error:

TypeError: 'Catalog' object is not subscriptable

I could see a number of people making this mistake or it being a difficult to handle bug in some automated process that reads catalogs. It would be nice if this error was handled such that a specific pystac exception is thrown when this happens or it automatically switches to the collection reader when a collection is detected.

@lossyrob lossyrob added the bug Things which are broken label Sep 16, 2020
@lossyrob
Copy link
Member

lossyrob commented Sep 16, 2020

This should be handled better so that the error is more interpretable.

It may be worth it to run a check using identify_stac_object_type in all the specific object reads to make sure the user is doing the right thing.

Also note that pystac.read_file will identify the object for you and give you back the relevant STAC object.

@lossyrob
Copy link
Member

Looking into this - I tried to replicate and could not. It's unclear where in the code this is throwing. Can you give reproduction steps?

@CloudNiner
Copy link
Contributor Author

Shoot, sorry for the confusion. It looks like it doesn't throw when the pystac.Catalog.from_file is called, but when you try to perform some operation on the catalog afterwards:

catalog = pystac.Catalog.from_file('catalog/collection.json')
catalog.describe()
* <Catalog id=hand_021>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-a1e8ca2d347c> in <module>
----> 1 catalog.describe()

~/.pyenv/versions/3.7.8/envs/pystac-0.5.2/lib/python3.7/site-packages/pystac/catalog.py in describe(self, include_hrefs, _indent)
    617         for child in self.get_children():
    618             child.describe(include_hrefs=include_hrefs, _indent=_indent + 4)
--> 619         for item in self.get_items():
    620             s = '{}* {}'.format(' ' * (_indent + 2), item)
    621             if include_hrefs:

~/.pyenv/versions/3.7.8/envs/pystac-0.5.2/lib/python3.7/site-packages/pystac/stac_object.py in get_stac_objects(self, rel)
    360             link = links[i]
    361             if link.rel == rel:
--> 362                 link.resolve_stac_object(root=self.get_root())
    363                 yield link.target
    364 

~/.pyenv/versions/3.7.8/envs/pystac-0.5.2/lib/python3.7/site-packages/pystac/link.py in resolve_stac_object(self, root)
    158                 target_href = make_absolute_href(target_href, start_href)
    159 
--> 160             obj = STAC_IO.read_stac_object(target_href, root=root)
    161             obj.set_self_href(target_href)
    162         else:

~/.pyenv/versions/3.7.8/envs/pystac-0.5.2/lib/python3.7/site-packages/pystac/stac_io.py in read_stac_object(cls, uri, root)
    129         """
    130         d = cls.read_json(uri)
--> 131         return cls.stac_object_from_dict(d, href=uri, root=root)
    132 
    133     @classmethod

~/.pyenv/versions/3.7.8/envs/pystac-0.5.2/lib/python3.7/site-packages/pystac/serialization/__init__.py in stac_object_from_dict(d, href, root)
     28 
     29         # Merge common properties in case this is an older STAC object.
---> 30         merge_common_properties(d, json_href=href, collection_cache=collection_cache)
     31 
     32     info = identify_stac_object(d)

~/.pyenv/versions/3.7.8/envs/pystac-0.5.2/lib/python3.7/site-packages/pystac/serialization/common_properties.py in merge_common_properties(item_dict, collection_cache, json_href)
     73             collection_props = collection.properties
     74         else:
---> 75             collection_id = collection['id']
     76             if 'properties' in collection:
     77                 collection_props = collection['properties']

TypeError: 'Catalog' object is not subscriptable

Here's the catalog I was using:
catalog.tar.gz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Things which are broken
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants