From 1e0130de1ab3469ce73fd087b4abdcf482a9d871 Mon Sep 17 00:00:00 2001 From: Alexander Pace <alexander.pace@ligo.org> Date: Thu, 27 Jul 2023 16:29:24 +0000 Subject: [PATCH] catch query KeyError and return an informative message to the user --- gracedb/api/v1/superevents/filters.py | 4 +++- gracedb/search/forms.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/gracedb/api/v1/superevents/filters.py b/gracedb/api/v1/superevents/filters.py index 8ff62a004..0e2ed9668 100644 --- a/gracedb/api/v1/superevents/filters.py +++ b/gracedb/api/v1/superevents/filters.py @@ -6,6 +6,7 @@ from django.http import HttpResponseBadRequest from rest_framework import filters, exceptions +from search.forms import se_gpstime_parseerror from search.query.labels import filter_for_labels from search.query.superevents import parseSupereventQuery @@ -40,7 +41,8 @@ class SupereventSearchFilter(filters.SearchFilter): qs = filter_for_labels(qs, query).distinct() except ParseException as e: raise exceptions.ParseError('Invalid query') - + except KeyError as e: + raise exceptions.ParseError(se_gpstime_parseerror(e)) return qs diff --git a/gracedb/search/forms.py b/gracedb/search/forms.py index a5ebe715d..fa93ebbe8 100644 --- a/gracedb/search/forms.py +++ b/gracedb/search/forms.py @@ -21,6 +21,18 @@ logger = logging.getLogger(__name__) htmlEntityStar = "★" errorMarker = '<span style="color:red;">'+htmlEntityStar+'</span>' +# Helpful status messages: +created_vs_t0 = 'Invalid query. Hint: date queries on the created: field take the form YYYY-MM-DD .. ' \ + 'YYYY-MM-DD. Date queries with gpstime are supported using the t_0: field' + + +# Helper function to capture superevent created vs t_0 gpstime queries +# and return it to the user: +def se_gpstime_parseerror(err): + if "ParseResults" in str(err) and 'miltime' in str(err): + return created_vs_t0 + else: + return err class MainSearchForm(forms.Form): QUERY_TYPE_EVENT = 'E' @@ -85,6 +97,8 @@ class MainSearchForm(forms.Form): err = "Error: invalid query. (" + escape(e.pstr[:e.loc]) + \ errorMarker + escape(e.pstr[e.loc:]) + ")" raise forms.ValidationError({'query': mark_safe(err)}) + except KeyError as e: + raise forms.ValidationError(se_gpstime_parseerror(e)) except Exception as e: # What could this be and how can we handle it better? XXX logger.error('{t}: {e}'.format(t=str(type(e)), e=str(e))) -- GitLab