Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Return status 404 instead of 500 when media not found (#3552)
Dhruv Bhanushalid8a60ad12a75
diff --git a/api/api/controllers/elasticsearch/related.py b/api/api/controllers/elasticsearch/related.pyindex 50d321695..ef7b2f4e4 100644--- a/api/api/controllers/elasticsearch/related.py+++ b/api/api/controllers/elasticsearch/related.py@@ -27,6 +27,8 @@ def related_media(uuid: str, index: str, filter_dead: bool) -> list[Hit]:# Search the default index for the item itself as it might be sensitive.item_search = Search(index=index)+ # This will raise ``IndexError`` if no hits are found. This error is caught+ # in the viewset handler function.item_hit = item_search.query(Term(identifier=uuid)).execute().hits[0]# Match related using title.diff --git a/api/api/views/media_views.py b/api/api/views/media_views.pyindex 6ce83f0a6..ace71bb86 100644--- a/api/api/views/media_views.py+++ b/api/api/views/media_views.py@@ -3,7 +3,7 @@ from typing import Unionfrom rest_framework import statusfrom rest_framework.decorators import action-from rest_framework.exceptions import APIException+from rest_framework.exceptions import APIException, NotFoundfrom rest_framework.response import Responsefrom rest_framework.viewsets import ReadOnlyModelViewSet@@ -276,7 +276,7 @@ class MediaViewSet(AsyncViewSetMixin, AsyncAPIView, ReadOnlyModelViewSet):raise APIException(getattr(e, "message", str(e)))# If there are no hits in the search controllerexcept IndexError:- raise APIException("Could not find items.", 404)+ raise NotFoundserializer_context = self.get_serializer_context()