From 31ca4f3d23f00320dab9a41ba024d9f1650c4ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 12 Jan 2018 15:27:56 +0100 Subject: [PATCH] Gracefully handle Elastic errors in user search --- pillar/api/search/routes.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pillar/api/search/routes.py b/pillar/api/search/routes.py index ff902ebe..941b9c33 100644 --- a/pillar/api/search/routes.py +++ b/pillar/api/search/routes.py @@ -1,6 +1,7 @@ import logging from flask import Blueprint, request +import elasticsearch.exceptions as elk_ex from werkzeug import exceptions as wz_exceptions from pillar.api.utils import authorization, jsonify @@ -61,7 +62,13 @@ def search_user(): page_idx = _page_index() # result is the raw elasticseach output. # we need to filter fields in case of user objects. - result = queries.do_user_search(searchword, terms, page_idx) + + try: + result = queries.do_user_search(searchword, terms, page_idx) + except elk_ex.ElasticsearchException as ex: + resp = jsonify({'_message': str(ex)}) + resp.status_code = 500 + return resp # filter sensitive stuff # we only need. objectID, full_name, username @@ -97,6 +104,12 @@ def search_user_admin(): searchword = _valid_search() terms = _term_filters() page_idx = _page_index() - result = queries.do_user_search_admin(searchword, terms, page_idx) + + try: + result = queries.do_user_search_admin(searchword, terms, page_idx) + except elk_ex.ElasticsearchException as ex: + resp = jsonify({'_message': str(ex)}) + resp.status_code = 500 + return resp return jsonify(result)