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

added error handling to external queries #616

Merged
merged 2 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

#### Fixed

- Added an error handler for the external source queries (e.g. SIMBAD) [#616](https://github.com/askap-vast/vast-pipeline/pull/616).
- Stopped JS9 from changing the page titles [#597](https://github.com/askap-vast/vast-pipeline/pull/597).
- Fixed regression issues with pandas 1.4 [#586](https://github.com/askap-vast/vast-pipeline/pull/586).
- Fixed config being copied before run was confirmed to actually go ahead for existing runs [#595](https://github.com/askap-vast/vast-pipeline/pull/595).
Expand All @@ -69,6 +70,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

#### List of PRs

- [#616](https://github.com/askap-vast/vast-pipeline/pull/616): fix: added error handling to external queries.
- [#614](https://github.com/askap-vast/vast-pipeline/pull/614): feat: Refactored add to favourites button to avoid refresh.
- [#597](https://github.com/askap-vast/vast-pipeline/pull/597): fix: Update detail page titles.
- [#586](https://github.com/askap-vast/vast-pipeline/pull/586): feat, dep, doc: Add an eta-v analysis page for the source query.
Expand Down
7 changes: 6 additions & 1 deletion vast_pipeline/utils/external_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ def simbad(coord: SkyCoord, radius: Angle) -> List[Dict[str, Any]]:
"otype(V)",
"otypes",
)
simbad_result_table = CustomSimbad.query_region(coord, radius=radius)
try:
simbad_result_table = CustomSimbad.query_region(coord, radius=radius)
except requests.HTTPError:
# try the Harvard mirror
CustomSimbad.SIMBAD_URL = "https://simbad.harvard.edu/simbad/sim-script"
simbad_result_table = CustomSimbad.query_region(coord, radius=radius)
if simbad_result_table is None:
simbad_results_dict_list = []
else:
Expand Down
28 changes: 25 additions & 3 deletions vast_pipeline/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

from django_q.tasks import async_task

import requests
from rest_framework import status
import rest_framework.decorators
from rest_framework.request import Request
Expand Down Expand Up @@ -2466,6 +2467,21 @@ class UtilitiesSet(ViewSet):
authentication_classes = [SessionAuthentication, BasicAuthentication]
permission_classes = [IsAuthenticated]

@staticmethod
def _external_search_error_handler(
external_query_func,
coord: SkyCoord,
radius: Angle,
service_name: str,
request: Request,
) -> List[Dict[str, Any]]:
try:
results = external_query_func(coord, radius)
except requests.HTTPError:
messages.error(request, f"Unable to get {service_name} query results.")
results = []
return results

@rest_framework.decorators.action(methods=['get'], detail=False)
def sesame_search(self, request: Request) -> Response:
"""Query the Sesame name resolver service and return a coordinate.
Expand Down Expand Up @@ -2560,9 +2576,15 @@ def external_search(self, request: Request) -> Response:
except ValueError as e:
raise serializers.ValidationError({"radius": str(e.args[0])})

simbad_results = external_query.simbad(coord, radius)
ned_results = external_query.ned(coord, radius)
tns_results = external_query.tns(coord, radius)
simbad_results = self._external_search_error_handler(
external_query.simbad, coord, radius, "SIMBAD", request
)
ned_results = self._external_search_error_handler(
external_query.ned, coord, radius, "NED", request
)
tns_results = self._external_search_error_handler(
external_query.tns, coord, radius, "TNS", request
)

results = simbad_results + ned_results + tns_results
serializer = ExternalSearchSerializer(data=results, many=True)
Expand Down