Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix thumbnail url crash for id of not existsing instance (#10384)

Iga Karbowiakd343371d5972
diff --git a/saleor/thumbnail/tests/test_views.py b/saleor/thumbnail/tests/test_views.py
index 0b2b13b92b..c96daf2924 100644
--- a/saleor/thumbnail/tests/test_views.py
+++ b/saleor/thumbnail/tests/test_views.py
@@ -230,3 +230,15 @@ def test_handle_thumbnail_view_invalid_instance_id(client, category):
# then
assert response.status_code == 404
+
+
+def test_handle_thumbnail_view_object_does_not_exists(client):
+ # given
+ size = 60
+ category_id = graphene.Node.to_global_id("Category", 1)
+
+ # when
+ response = client.get(f"/thumbnail/{category_id}/{size}/")
+
+ # then
+ assert response.status_code == 404
diff --git a/saleor/thumbnail/views.py b/saleor/thumbnail/views.py
index cf19643532..c842cae77d 100644
--- a/saleor/thumbnail/views.py
+++ b/saleor/thumbnail/views.py
@@ -1,5 +1,6 @@
from collections import namedtuple
+from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponseNotFound, HttpResponseRedirect
from graphql.error import GraphQLError
@@ -52,7 +53,11 @@ def handle_thumbnail(request, instance_id: str, size: str, format: str = None):
).first():
return HttpResponseRedirect(thumbnail.image.url)
- instance = model_data.model.objects.get(id=pk)
+ try:
+ instance = model_data.model.objects.get(id=pk)
+ except ObjectDoesNotExist:
+ return HttpResponseNotFound("Instance with the given id cannot be found.")
+
image = getattr(instance, model_data.image_field)
if not bool(image):
return HttpResponseNotFound("There is no image for provided instance.")