Skip to content

Commit 6c492c0

Browse files
committed
Report an error during tag browse if there are no sounds in solr
This caused an uncaught exception. Also fix an exception if there is a connection error to solr
1 parent 3e6e242 commit 6c492c0

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

tags/views.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
from django.http import Http404, HttpResponsePermanentRedirect, HttpResponseRedirect
2626
from django.shortcuts import render
2727
from django.urls import reverse
28+
import sentry_sdk
2829

2930
from search.views import search_view_helper
3031
from tags.models import Tag, FS1Tag
32+
from utils.search import SearchEngineException
3133
from utils.search.search_sounds import perform_search_engine_query
3234

3335
search_logger = logging.getLogger("search")
@@ -61,18 +63,26 @@ def tags(request, multiple_tags=None):
6163
if 'sqp' in tvars and not tvars['sqp'].get_tags_in_filters():
6264
initial_tagcloud = cache.get('initial_tagcloud')
6365
if initial_tagcloud is None:
64-
# If tagcloud is not cached, make a query to retrieve it and save it to cache
65-
results, _ = perform_search_engine_query(dict(
66-
textual_query='',
67-
query_filter= "*:*",
68-
num_sounds=1,
69-
facets={settings.SEARCH_SOUNDS_FIELD_TAGS: {'limit': 200}},
70-
group_by_pack=True,
71-
group_counts_as_one_in_facets=False,
72-
))
73-
initial_tagcloud = [dict(name=f[0], count=f[1], browse_url=reverse('tags', args=[f[0]])) for f in results.facets[settings.SEARCH_SOUNDS_FIELD_TAGS]]
74-
cache.set('initial_tagcloud', initial_tagcloud, 60 * 60 * 12) # cache for 12 hours
75-
tvars.update({'initial_tagcloud': initial_tagcloud})
66+
try:
67+
# If tagcloud is not cached, make a query to retrieve it and save it to cache
68+
results, _ = perform_search_engine_query(dict(
69+
textual_query='',
70+
query_filter= "*:*",
71+
num_sounds=1,
72+
facets={settings.SEARCH_SOUNDS_FIELD_TAGS: {'limit': 200}},
73+
group_by_pack=True,
74+
group_counts_as_one_in_facets=False,
75+
))
76+
if settings.SEARCH_SOUNDS_FIELD_TAGS in results.facets:
77+
initial_tagcloud = [dict(name=f[0], count=f[1], browse_url=reverse('tags', args=[f[0]])) for f in results.facets.get(settings.SEARCH_SOUNDS_FIELD_TAGS, [])]
78+
cache.set('initial_tagcloud', initial_tagcloud, 60 * 60 * 12) # cache for 12 hours
79+
tvars.update({'initial_tagcloud': initial_tagcloud})
80+
else:
81+
tvars.update({'error_text': 'There was an error while loading results, please try again later.'})
82+
except SearchEngineException as e:
83+
search_logger.info(f'Tag browse error: Could probably not connect to Solr - {e}')
84+
sentry_sdk.capture_exception(e) # Manually capture exception so it has more info and Sentry can organize it properly
85+
tvars.update({'error_text': 'The search server could not be reached, please try again later.'})
7686

7787
return render(request, 'search/search.html', tvars)
7888

0 commit comments

Comments
 (0)