Skip to content

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

Handle RequestDataTooBig error (#19179)

Lukasz Ostrowski93ddf1bda760
diff --git a/saleor/graphql/core/tests/test_view.py b/saleor/graphql/core/tests/test_view.py
index e5e2d0bd11..bcae5185bb 100644
--- a/saleor/graphql/core/tests/test_view.py
+++ b/saleor/graphql/core/tests/test_view.py
@@ -244,6 +244,22 @@ def test_unexpected_types_in_json_request_body(client, data):
assert errors[0]["message"] == "Unable to parse query."
+def test_request_body_too_big(client, settings):
+ # given
+ settings.DATA_UPLOAD_MAX_MEMORY_SIZE = 10
+ data = '{"query": "query { products(first: 10) { edges { node { id } } } }"}'
+
+ # when
+ response = client.post(API_PATH, data, content_type="application/json")
+
+ # then
+ assert response.status_code == 413
+ content = get_graphql_content_from_response(response)
+ errors = content.get("errors")
+ assert len(errors) == 1
+ assert errors[0]["message"] == "Request body exceeds maximum size."
+
+
def test_invalid_query(api_client):
query = "query { invalid }"
response = api_client.post_graphql(query, check_no_permissions=False)
diff --git a/saleor/graphql/views.py b/saleor/graphql/views.py
index 7d55bb55fb..a7c2316252 100644
--- a/saleor/graphql/views.py
+++ b/saleor/graphql/views.py
@@ -9,6 +9,7 @@ from urllib.parse import urljoin
import orjson
from django.conf import settings
from django.core.cache import cache
+from django.core.exceptions import RequestDataTooBig
from django.http import HttpRequest, HttpResponse, HttpResponseNotAllowed
from django.shortcuts import render
from django.views.generic import View
@@ -171,6 +172,17 @@ class GraphQLView(View):
def _handle_query(self, request: HttpRequest) -> JsonResponse:
try:
data = self.parse_body(request)
+ except RequestDataTooBig:
+ return JsonResponse(
+ data={
+ "errors": [
+ self.format_error(
+ GraphQLError("Request body exceeds maximum size.")
+ )
+ ]
+ },
+ status=413,
+ )
except ValueError:
return JsonResponse(
data={