Skip to content

Commit 2ac0cb8

Browse files
authored
Merge pull request #2608 from bsipocz/xmatch_parse_error_from_votable
ENH: exposing error returned in a VOTable
2 parents 96e1476 + a3a2552 commit 2ac0cb8

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ svo_fps
155155
- The default wavelength range used by ``get_filter_index()`` was far too
156156
large. The user must now always specify both upper and lower limits. [#2509]
157157

158+
xmatch
159+
^^^^^^
160+
161+
- The reason for the query errors, as parsed from the returned VOTable is now
162+
exposed as part of the traceback. [#2608]
163+
158164
gaia
159165
^^^^
160166

astroquery/xmatch/core.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22

3-
from io import StringIO
3+
from io import StringIO, BytesIO
44

5-
from astropy.io import ascii
5+
from astropy.io import ascii, votable
66
import astropy.units as u
77
from astropy.table import Table
8+
from requests import HTTPError
89

9-
from . import conf
10-
from ..query import BaseQuery
11-
from ..utils import url_helpers, prepend_docstr_nosections, async_to_sync
1210

11+
from astroquery.query import BaseQuery
12+
from astroquery.exceptions import InvalidQueryError
13+
from astroquery.utils import url_helpers, prepend_docstr_nosections, async_to_sync
14+
15+
from . import conf
1316
try:
1417
from regions import CircleSkyRegion
1518
except ImportError:
@@ -106,7 +109,12 @@ def query_async(self, cat1, cat2, max_distance, colRA1=None, colDec1=None,
106109

107110
response = self._request(method='POST', url=self.URL, data=payload,
108111
timeout=self.TIMEOUT, cache=cache, **kwargs)
109-
response.raise_for_status()
112+
try:
113+
response.raise_for_status()
114+
except HTTPError as err:
115+
error_votable = votable.parse(BytesIO(response.content))
116+
error_reason = error_votable.get_info_by_id('QUERY_STATUS').content
117+
raise InvalidQueryError(error_reason) from err
110118

111119
return response
112120

astroquery/xmatch/tests/test_xmatch_remote.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
except ImportError:
1818
pass
1919

20+
from astroquery.exceptions import InvalidQueryError
2021
from ...xmatch import XMatch
2122

2223

@@ -111,3 +112,13 @@ def test_xmatch_query_with_cone_area(self, xmatch):
111112
except ReadTimeout:
112113
pytest.xfail("xmatch query timed out.")
113114
assert len(table) == 185
115+
116+
def test_xmatch_invalid_query(self, xmatch):
117+
input_table = Table.read(DATA_DIR / "posList.csv", format="ascii.csv")
118+
# columns in input table and kwargs are not matching
119+
120+
with pytest.raises(InvalidQueryError) as err:
121+
xmatch.query(input_table, cat2='vizier:II/246/out', max_distance=5 * arcsec,
122+
colRA1='ra', colDec1='DEC')
123+
124+
assert 'Column name "DEC" not found in table metadata' in str(err)

0 commit comments

Comments
 (0)