Skip to content

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

Fix error handling and parse errors when dealing with unexpected and/or incorrect JSON payloads (#19016)

Marcin Wcisłoe7c505a0ddff
diff --git a/saleor/graphql/core/tests/test_view.py b/saleor/graphql/core/tests/test_view.py
index 5201ba6219..3c211c3180 100644
--- a/saleor/graphql/core/tests/test_view.py
+++ b/saleor/graphql/core/tests/test_view.py
@@ -96,12 +96,17 @@ def test_graphql_view_not_allowed(method, client):
assert response.status_code == 405
+@override_settings(DEBUG=False)
def test_invalid_request_body_non_debug(client):
data = "invalid-data"
response = client.post(API_PATH, data, content_type="application/json")
assert response.status_code == 400
content = get_graphql_content_from_response(response)
- assert "errors" in content
+ errors = content.get("errors")
+ assert len(errors) == 1
+ assert errors[0]["message"] == "Unable to parse query."
+ assert errors[0]["extensions"]["exception"]["code"] == "GraphQLError"
+ assert "stacktrace" not in errors[0]["extensions"]["exception"]
@override_settings(DEBUG=True)
@@ -111,12 +116,47 @@ def test_invalid_request_body_with_debug(client):
assert response.status_code == 400
content = get_graphql_content_from_response(response)
errors = content.get("errors")
- assert errors == [
- {
- "extensions": {"exception": {"code": "str", "stacktrace": []}},
- "message": "Unable to parse query.",
- }
- ]
+ assert len(errors) == 1
+ assert errors[0]["message"] == "Unable to parse query."
+ assert errors[0]["extensions"]["exception"]["code"] == "GraphQLError"
+ assert "stacktrace" in errors[0]["extensions"]["exception"]
+
+
[email protected]("debug", [True, False])
+def test_invalid_request_body_error_is_not_logged(client, caplog, settings, debug):
+ # given
+ settings.DEBUG = debug
+ data = "invalid-data"
+
+ # when
+ with caplog.at_level(logging.ERROR, logger="saleor.graphql.errors.unhandled"):
+ client.post(API_PATH, data, content_type="application/json")
+
+ # then
+ assert caplog.records == []
+
+
+ "data",
+ [
+ '""',
+ "123",
+ "1.5",
+ "true",
+ "false",
+ "null",
+ ],
+)
+def test_unexpected_types_in_json_request_body(client, data):
+ # when
+ response = client.post(API_PATH, data, content_type="application/json")
+
+ # then
+ content = get_graphql_content_from_response(response)
+ assert response.status_code == 400
+ errors = content.get("errors")
+ assert len(errors) == 1
+ assert errors[0]["message"] == "Unable to parse query."
def test_invalid_query(api_client):
diff --git a/saleor/graphql/views.py b/saleor/graphql/views.py
index 164e8b9677..327f5ae8d8 100644
--- a/saleor/graphql/views.py
+++ b/saleor/graphql/views.py
@@ -171,7 +171,11 @@ class GraphQLView(View):
data = self.parse_body(request)
except ValueError:
return JsonResponse(
- data={"errors": [self.format_error("Unable to parse query.")]},
+ data={
+ "errors": [
+ self.format_error(GraphQLError("Unable to parse query."))
+ ]
+ },
status=400,
)
@@ -486,8 +490,11 @@ class GraphQLView(View):
if content_type == "application/graphql":
return {"query": request.body.decode("utf-8")}
if content_type == "application/json":
- body = request.body
- return orjson.loads(body)
+ body = orjson.loads(request.body)
+ if isinstance(body, dict) or isinstance(body, list):
+ return body
+
+ raise ValueError("Invalid query.")
if content_type in ["application/x-www-form-urlencoded", "multipart/form-data"]:
return request.POST
return {}