Skip to content

Commit 18772c5

Browse files
committed
Better exception handling, fix README code
fix JSONDecodeError import
1 parent 9d659e7 commit 18772c5

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

astroquery/mast/missions.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
"""
88

99
import difflib
10+
from json import JSONDecodeError
1011
import warnings
1112

1213
from astropy.table import Table
1314
import astropy.units as u
1415
import astropy.coordinates as coord
16+
from requests import RequestException
1517

1618
from astroquery.utils import commons, async_to_sync
1719
from astroquery.utils.class_or_instance import class_or_instance
@@ -292,8 +294,18 @@ def get_column_list(self):
292294
# Create Table with parsed data
293295
col_table = Table(rows=rows, names=('name', 'data_type', 'description'))
294296
self.columns[self.mission] = col_table
297+
except JSONDecodeError as ex:
298+
raise JSONDecodeError(f'Failed to decode JSON response while attempting to get column list'
299+
f' for mission {self.mission}: {ex}')
300+
except RequestException as ex:
301+
raise ConnectionError(f'Failed to connect to the server while attempting to get column list'
302+
f' for mission {self.mission}: {ex}')
303+
except KeyError as ex:
304+
raise KeyError(f'Expected key not found in response data while attempting to get column list'
305+
f' for mission {self.mission}: {ex}')
295306
except Exception as ex:
296-
raise RuntimeError(f'Error occurred while trying to get column list for mission {self.mission}: {ex}')
307+
raise RuntimeError(f'An unexpected error occurred while attempting to get column list'
308+
f' for mission {self.mission}: {ex}')
297309

298310
return self.columns[self.mission]
299311

astroquery/mast/tests/data/README.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ This directory contains sample data that is used to mock functions in `~astroque
66

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

9-
.. code-block:: python
9+
.. doctest-remote-data::
1010

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

0 commit comments

Comments
 (0)