Skip to content

Commit

Permalink
Better exception handling, fix README code
Browse files Browse the repository at this point in the history
  • Loading branch information
snbianco committed Nov 6, 2024
1 parent 75b281f commit 2b3ef26
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
13 changes: 12 additions & 1 deletion astroquery/mast/missions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from astropy.table import Table
import astropy.units as u
import astropy.coordinates as coord
from requests import JSONDecodeError, RequestException

from astroquery.utils import commons, async_to_sync
from astroquery.utils.class_or_instance import class_or_instance
Expand Down Expand Up @@ -292,8 +293,18 @@ def get_column_list(self):
# Create Table with parsed data
col_table = Table(rows=rows, names=('name', 'data_type', 'description'))
self.columns[self.mission] = col_table
except JSONDecodeError as ex:
raise JSONDecodeError(f'Failed to decode JSON response while attempting to get column list'

Check warning on line 297 in astroquery/mast/missions.py

View check run for this annotation

Codecov / codecov/patch

astroquery/mast/missions.py#L296-L297

Added lines #L296 - L297 were not covered by tests
f' for mission {self.mission}: {ex}')
except RequestException as ex:
raise ConnectionError(f'Failed to connect to the server while attempting to get column list'

Check warning on line 300 in astroquery/mast/missions.py

View check run for this annotation

Codecov / codecov/patch

astroquery/mast/missions.py#L299-L300

Added lines #L299 - L300 were not covered by tests
f' for mission {self.mission}: {ex}')
except KeyError as ex:
raise KeyError(f'Expected key not found in response data while attempting to get column list'

Check warning on line 303 in astroquery/mast/missions.py

View check run for this annotation

Codecov / codecov/patch

astroquery/mast/missions.py#L302-L303

Added lines #L302 - L303 were not covered by tests
f' for mission {self.mission}: {ex}')
except Exception as ex:
raise RuntimeError(f'Error occurred while trying to get column list for mission {self.mission}: {ex}')
raise RuntimeError(f'An unexpected error occurred while attempting to get column list'

Check warning on line 306 in astroquery/mast/missions.py

View check run for this annotation

Codecov / codecov/patch

astroquery/mast/missions.py#L305-L306

Added lines #L305 - L306 were not covered by tests
f' for mission {self.mission}: {ex}')

return self.columns[self.mission]

Expand Down
9 changes: 4 additions & 5 deletions astroquery/mast/tests/data/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ This directory contains sample data that is used to mock functions in `~astroque

To generate `~astroquery.mast.tests.data.mission_columns.json`, use the following:

.. code-block:: python
.. doctest-remote-data::

>>> import json
>>> from astroquery.mast import utils
>>> params = {'mission': 'hst'}
>>> resp = utils._simple_request(f'https://mast.stsci.edu/search/util/api/v0.1/column_list', {'mission': 'hst'})
...
>>> resp = utils._simple_request('https://mast.stsci.edu/search/util/api/v0.1/column_list', {'mission': 'hst'})
>>> with open('mission_columns.json', 'w') as file:
>>> json.dump(resp.json(), file, indent=4)
... json.dump(resp.json(), file, indent=4)

0 comments on commit 2b3ef26

Please sign in to comment.