Skip to content

Commit 816ae98

Browse files
committed
chg: [search] catch unreachable and timeout error
1 parent 691a248 commit 816ae98

3 files changed

Lines changed: 52 additions & 4 deletions

File tree

bin/lib/search_engine.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,22 @@ def api_search(data):
526526
except:
527527
return {"status": "error", "reason": "Invalid date from"}, 400
528528

529-
result = Engine.search(indexes, to_search, page=page, nb=nb_per_page, timestamp_from=timestamp_from, sort=sort,
530-
timestamp_to=timestamp_to)
529+
try:
530+
result = Engine.search(indexes, to_search, page=page, nb=nb_per_page, timestamp_from=timestamp_from, sort=sort,
531+
timestamp_to=timestamp_to)
532+
except MeilisearchTimeoutError:
533+
return {
534+
"status": "error",
535+
"error_type": "meilisearch_timeout",
536+
"reason": "The connection to Meilisearch timed out. Please try again in a few minutes."
537+
}, 503
538+
except MeilisearchCommunicationError:
539+
return {
540+
"status": "error",
541+
"error_type": "meilisearch_unreachable",
542+
"reason": "Meilisearch is unreachable. Install/start Meilisearch or check the connection settings."
543+
}, 503
544+
531545
objs = []
532546
pagination = {}
533547
if result.get("hits"):

var/www/blueprints/search_b.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def create_json_response(data, status_code):
3838
def log(user_id, index, to_search):
3939
logger.warning(f'{user_id} search: {index} - {to_search}')
4040

41+
4142
# ============ FUNCTIONS ============
4243

4344
# ============= ROUTES ==============
@@ -65,12 +66,30 @@ def search_dashboard():
6566

6667
# selected_scopes -> scope_human
6768

69+
search_error = None
6870
if search:
6971
r = search_engine.api_search({'indexes': indexes, 'search': search, 'page': page, 'user_id': user_id,
7072
'from': last_seen_from, 'to': last_seen_to, 'sort': sort})
7173
if r[1] != 200:
72-
return create_json_response(r[0], r[1])
73-
result, pagination = r[0]
74+
error_type = r[0].get('error_type')
75+
if error_type == 'meilisearch_timeout':
76+
search_error = {
77+
'title': 'Search service timeout',
78+
'message': r[0].get('reason')
79+
}
80+
result = None
81+
pagination = None
82+
elif error_type == 'meilisearch_unreachable':
83+
search_error = {
84+
'title': 'Search service unavailable',
85+
'message': r[0].get('reason')
86+
}
87+
result = None
88+
pagination = None
89+
else:
90+
return create_json_response(r[0], r[1])
91+
else:
92+
result, pagination = r[0]
7493
else:
7594
result = None
7695
pagination = None
@@ -83,6 +102,7 @@ def search_dashboard():
83102
sort=sort,
84103
last_seen_from=last_seen_from,
85104
last_seen_to=last_seen_to,
105+
search_error=search_error,
86106
result=result, pagination=pagination)
87107

88108
# username_subtypes=ail_core.get_object_all_subtypes('username')

var/www/templates/search/search_dashboard.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,20 @@ <h6 class="text-uppercase text-muted mb-3">Search scope</h6>
382382

383383
</form>
384384

385+
{% if search_error %}
386+
<div class="alert alert-warning border-left border-warning shadow-sm mt-3" role="alert">
387+
<div class="d-flex align-items-start">
388+
<div class="mr-3 mt-1">
389+
<i class="fas fa-exclamation-triangle fa-lg text-warning"></i>
390+
</div>
391+
<div>
392+
<h6 class="alert-heading mb-1">{{ search_error.title }}</h6>
393+
<p class="mb-0">{{ search_error.message }}</p>
394+
</div>
395+
</div>
396+
</div>
397+
{% endif %}
398+
385399
{# <small class="text-muted d-block mt-2">#}
386400
{# Examples: <code>@username</code>, <code>example.onion</code>, <code>invoice.pdf</code>, <code>ransomware</code>#}
387401
{# </small>#}

0 commit comments

Comments
 (0)