Skip to content

Commit a8154fd

Browse files
committed
fix for issue #2237: do not cache results that cannot be parsed.
1 parent 17c79f9 commit a8154fd

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

astroquery/jplhorizons/core.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from ..query import BaseQuery
2121
# async_to_sync generates the relevant query tools from _async methods
2222
from ..utils import async_to_sync
23+
from ..exceptions import TableParseError
2324
# import configurable items declared in __init__.py
2425
from . import conf
2526

@@ -1287,11 +1288,23 @@ def _parse_result(self, response, verbose=None):
12871288
data : `astropy.Table`
12881289
12891290
"""
1291+
self.last_response = response
12901292
if self.query_type not in ['ephemerides', 'elements', 'vectors']:
12911293
return None
12921294
else:
1293-
data = self._parse_horizons(response.text)
1294-
1295+
try:
1296+
data = self._parse_horizons(response.text)
1297+
except Exception as ex:
1298+
try:
1299+
self._last_query.remove_cache_file(self.cache_location)
1300+
except OSError:
1301+
# this is allowed: if `cache` was set to False, this
1302+
# won't be needed
1303+
pass
1304+
raise TableParseError("Failed to parse JPL Horizons result. "
1305+
"The raw response can be found in "
1306+
"`self.last_response`. Exception: "
1307+
+ str(ex)) from ex
12951308
return data
12961309

12971310

astroquery/jplhorizons/tests/data/README

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ ceres_vectors.txt
1010
https://ssd.jpl.nasa.gov/api/horizons.api?format=text&EPHEM_TYPE=VECTORS&OUT_UNITS=AU-D&COMMAND=%22Ceres%3B%22&CENTER=%27500%4010%27&CSV_FORMAT=%22YES%22&REF_PLANE=ECLIPTIC&REF_SYSTEM=ICRF&TP_TYPE=ABSOLUTE&VEC_LABELS=YES&VEC_CORR=%22NONE%22&VEC_DELTA_T=NO&OBJ_DATA=YES&TLIST=2451544.5
1111

1212
no_H.txt
13-
https://ssd.jpl.nasa.gov/api/horizons.api?format=text&EPHEM_TYPE=OBSERVER&QUANTITIES=%271%2C2%2C3%2C4%2C5%2C6%2C7%2C8%2C9%2C10%2C11%2C12%2C13%2C14%2C15%2C16%2C17%2C18%2C19%2C20%2C21%2C22%2C23%2C24%2C25%2C26%2C27%2C28%2C29%2C30%2C31%2C32%2C33%2C34%2C35%2C36%2C37%2C38%2C39%2C40%2C41%2C42%2C43%27&COMMAND=%221935+UZ%3B%22&SOLAR_ELONG=%220%2C180%22&LHA_CUTOFF=0&CSV_FORMAT=YES&CAL_FORMAT=BOTH&ANG_FORMAT=DEG&APPARENT=AIRLESS&REF_SYSTEM=ICRF&EXTRA_PREC=NO&CENTER=%27500%40399%27&TLIST=2459480.5004416634&SKIP_DAYLT=NO
13+
https://ssd.jpl.nasa.gov/api/horizons.api?format=text&EPHEM_TYPE=OBSERVER&QUANTITIES=%271%2C2%2C3%2C4%2C5%2C6%2C7%2C8%2C9%2C10%2C11%2C12%2C13%2C14%2C15%2C16%2C17%2C18%2C19%2C20%2C21%2C22%2C23%2C24%2C25%2C26%2C27%2C28%2C29%2C30%2C31%2C32%2C33%2C34%2C35%2C36%2C37%2C38%2C39%2C40%2C41%2C42%2C43%27&COMMAND=%221935+UZ%3B%22&SOLAR_ELONG=%220%2C180%22&LHA_CUTOFF=0&CSV_FORMAT=YES&CAL_FORMAT=BOTH&ANG_FORMAT=DEG&APPARENT=AIRLESS&REF_SYSTEM=ICRF&EXTRA_PREC=NO&CENTER=%27500%40399%27&TLIST=2459480.5004416634&SKIP_DAYLT=NO
14+
15+
tlist_error.txt
16+
https://ssd.jpl.nasa.gov/api/horizons.api?format=text&COMMAND=%27499%27&OBJ_DATA=%27YES%27&MAKE_EPHEM=%27YES%27&EPHEM_TYPE=%27OBSERVER%27&CENTER=%27500@399%27&QUANTITIES=%271,9,20,23,24,29%27&TLIST=[]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
API VERSION: 1.1
2+
API SOURCE: NASA/JPL Horizons API
3+
4+
BATVAR: no TLIST values found (or missing quotes)
5+
WLDINI: error loading execution-control file.

astroquery/jplhorizons/tests/test_jplhorizons.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
from astropy.utils.exceptions import AstropyDeprecationWarning
1010

1111
from ...utils.testing_tools import MockResponse
12+
from ...query import AstroQuery
13+
from ...exceptions import TableParseError
1214
from ... import jplhorizons
1315

1416
# files in data/ for different query types
1517
DATA_FILES = {'ephemerides': 'ceres_ephemerides.txt',
1618
'elements': 'ceres_elements.txt',
1719
'vectors': 'ceres_vectors.txt',
18-
'"1935 UZ"': 'no_H.txt'}
20+
'"1935 UZ"': 'no_H.txt',
21+
'"tlist_error"': 'tlist_error.txt'}
1922

2023

2124
def data_path(filename):
@@ -55,6 +58,13 @@ def patch_request(request):
5558

5659
# --------------------------------- actual test functions
5760

61+
def test_parse_result(patch_request):
62+
q = jplhorizons.Horizons(id='tlist_error')
63+
# need _last_query to be defined
64+
q._last_query = AstroQuery('GET', 'http://dummy')
65+
with pytest.raises(TableParseError):
66+
res = q.ephemerides()
67+
5868

5969
def test_ephemerides_query(patch_request):
6070
# check values of Ceres for a given epoch

0 commit comments

Comments
 (0)