From 76cec8852f6e431b1c87f7d25f8bd4404f1c67e1 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 28 Jul 2020 16:50:24 -0700 Subject: search: catch ES errors and display better --- python/fatcat_web/routes.py | 12 +++++++--- python/fatcat_web/search.py | 22 ++++++++++++++---- python/fatcat_web/templates/container_search.html | 2 ++ python/fatcat_web/templates/release_search.html | 2 ++ python/fatcat_web/templates/search_macros.html | 28 ++++++++++++----------- 5 files changed, 46 insertions(+), 20 deletions(-) diff --git a/python/fatcat_web/routes.py b/python/fatcat_web/routes.py index 4a66b3c2..77b5dc55 100644 --- a/python/fatcat_web/routes.py +++ b/python/fatcat_web/routes.py @@ -14,7 +14,7 @@ from fatcat_tools.normal import * from fatcat_web import app, api, auth_api, priv_api, mwoauth, Config from fatcat_web.auth import handle_token_login, handle_logout, load_user, handle_ia_xauth, handle_wmoauth from fatcat_web.cors import crossdomain -from fatcat_web.search import ReleaseQuery, GenericQuery, do_release_search, do_container_search, get_elastic_entity_stats, get_elastic_container_stats, get_elastic_container_histogram +from fatcat_web.search import ReleaseQuery, GenericQuery, do_release_search, do_container_search, get_elastic_entity_stats, get_elastic_container_stats, get_elastic_container_histogram, FatcatSearchError from fatcat_web.entity_helpers import * from fatcat_web.graphics import * from fatcat_web.kafka import * @@ -710,7 +710,10 @@ def release_search(): return render_template('release_search.html', query=ReleaseQuery(), found=None) query = ReleaseQuery.from_args(request.args) - found = do_release_search(query) + try: + found = do_release_search(query) + except FatcatSearchError as fse: + return render_template('release_search.html', query=query, es_error=fse), fse.status_code return render_template('release_search.html', query=query, found=found) @app.route('/container/search', methods=['GET', 'POST']) @@ -720,7 +723,10 @@ def container_search(): return render_template('container_search.html', query=GenericQuery(), found=None) query = GenericQuery.from_args(request.args) - found = do_container_search(query) + try: + found = do_container_search(query) + except FatcatSearchError as fse: + return render_template('container_search.html', query=query, es_error=fse), fse.status_code return render_template('container_search.html', query=query, found=found) def get_changelog_stats(): diff --git a/python/fatcat_web/search.py b/python/fatcat_web/search.py index fe0610e5..3fd7f9dc 100644 --- a/python/fatcat_web/search.py +++ b/python/fatcat_web/search.py @@ -15,6 +15,15 @@ import elasticsearch_dsl.response from fatcat_web import app +class FatcatSearchError(Exception): + + def __init__(self, status_code: int, name: str, description: str = None): + if status_code == "N/A": + status_code = 503 + self.status_code = status_code + self.name = name + self.description = description + @dataclass class ReleaseQuery: q: Optional[str] = None @@ -109,14 +118,19 @@ def wrap_es_execution(search: Search) -> Any: except elasticsearch.exceptions.RequestError as e: # this is a "user" error print("elasticsearch 400: " + str(e.info), file=sys.stderr) + description = None if e.info.get("error", {}).get("root_cause", {}): - raise ValueError(str(e.info["error"]["root_cause"][0].get("reason"))) - else: - raise ValueError(str(e.info)) + description = str(e.info["error"]["root_cause"][0].get("reason")) + raise FatcatSearchError(e.status_code, str(e.error), description) + except elasticsearch.exceptions.ConnectionError as e: + raise FatcatSearchError(e.status_code, "ConnectionError: search engine not available") except elasticsearch.exceptions.TransportError as e: # all other errors print("elasticsearch non-200 status code: {}".format(e.info), file=sys.stderr) - raise IOError(str(e.info)) + description = None + if e.info.get("error", {}).get("root_cause", {}): + description = str(e.info["error"]["root_cause"][0].get("reason")) + raise FatcatSearchError(e.status_code, str(e.error), description) return resp def do_container_search( diff --git a/python/fatcat_web/templates/container_search.html b/python/fatcat_web/templates/container_search.html index ce868991..bd92dc2b 100644 --- a/python/fatcat_web/templates/container_search.html +++ b/python/fatcat_web/templates/container_search.html @@ -80,6 +80,8 @@ {% endif %} +{% elif es_error %} + {{ search_macros.es_error_msg(es_error) }} {% endif %} diff --git a/python/fatcat_web/templates/release_search.html b/python/fatcat_web/templates/release_search.html index e004efc1..7fb475e3 100644 --- a/python/fatcat_web/templates/release_search.html +++ b/python/fatcat_web/templates/release_search.html @@ -74,6 +74,8 @@ {% endif %} +{% elif es_error %} + {{ search_macros.es_error_msg(es_error) }} {% endif %} diff --git a/python/fatcat_web/templates/search_macros.html b/python/fatcat_web/templates/search_macros.html index 383c271c..a207bfbc 100644 --- a/python/fatcat_web/templates/search_macros.html +++ b/python/fatcat_web/templates/search_macros.html @@ -44,23 +44,25 @@ found.count_returned }} out of {{ found.count_found }} results   {% macro es_error_msg(es_error) %}
- {% if es_error.status_code == 400 %} -
-
+
+
+ {% if es_error.status_code == 400 %} Query Error -
+ {% else %} + Search Index Error + {% if es_error.status_code %}({{ es_error.status_code }}){% endif %} + {% endif %} +
+ {% if es_error.description %}

Computer said: {{ es_error.description }} + {% elif es_error.name %} +

{{ es_error.name }} + {% endif %} + {% if es_error.status_code == 400 %}

Query parsing is currently very naive. Sometimes you can fix this problem by adding quotes around terms or entire phrases. -

- {% else %} -
-
- Search Index Error ({{ es_error.status_code }}) -
-

Computer said: {{ es_error.description }} -

- {% endif %} + {% endif %} +
{% endmacro %} -- cgit v1.2.3