Skip to content

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

Fixes after rebase

Aleksander Spyra1d0a908fcbb6
diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.py
index 6d9289c8b0..e702aea7cf 100644
--- a/saleor/graphql/account/mutations/base.py
+++ b/saleor/graphql/account/mutations/base.py
@@ -340,7 +340,12 @@ class BaseCustomerCreate(ModelMutation, I18nMixin):
if cleaned_input.get("send_password_email"):
if not cleaned_input.get("redirect_url"):
raise ValidationError(
- {"redirect_url": "Redirect url is required to send a password."}
+ {
+ "redirect_url": ValidationError(
+ "Redirect url is required to send a password.",
+ code=AccountErrorCode.REQUIRED,
+ )
+ }
)
validate_storefront_url(cleaned_input.get("redirect_url"))
diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.py
index deebf87d54..4fc1f2361e 100644
--- a/saleor/graphql/account/mutations/staff.py
+++ b/saleor/graphql/account/mutations/staff.py
@@ -175,7 +175,12 @@ class StaffCreate(ModelMutation):
if cleaned_input.get("send_password_email"):
if not cleaned_input.get("redirect_url"):
raise ValidationError(
- {"redirect_url": "Redirect url is required to send a password."}
+ {
+ "redirect_url": ValidationError(
+ "Redirect url is required to send a password.",
+ AccountErrorCode.REQUIRED,
+ )
+ }
)
validate_storefront_url(cleaned_input.get("redirect_url"))
diff --git a/saleor/graphql/core/mutations.py b/saleor/graphql/core/mutations.py
index 547b2c55af..adbfd97daf 100644
--- a/saleor/graphql/core/mutations.py
+++ b/saleor/graphql/core/mutations.py
@@ -21,7 +21,7 @@ from ..account.types import User
from ..utils import get_nodes
from .types import Error, MetaInput, MetaPath, Upload
from .utils import from_global_id_strict_type, snake_to_camel_case
-from .utils.error_codes import CommonErrorCode, get_error_code_from_error
+from .utils.error_codes import get_error_code_from_error
registry = get_global_registry()
@@ -173,8 +173,7 @@ class BaseMutation(graphene.Mutation):
raise ValidationError(
{
field: ValidationError(
- "Couldn't resolve to a node: %s" % node_id,
- code="object_does_not_exist",
+ "Couldn't resolve to a node: %s" % node_id, code="not_found"
)
}
)
diff --git a/saleor/graphql/core/utils/__init__.py b/saleor/graphql/core/utils/__init__.py
index 456627a793..38104fcd58 100644
--- a/saleor/graphql/core/utils/__init__.py
+++ b/saleor/graphql/core/utils/__init__.py
@@ -52,7 +52,9 @@ def from_global_id_strict_type(
) from exc
if str(_type) != str(only_type):
- raise ValidationError({field: f"Must receive a {only_type} id"})
+ raise ValidationError(
+ {field: ValidationError(f"Must receive a {only_type} id", code="invalid")}
+ )
return _id
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 5a58921bf7..16bf147574 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -49,7 +49,6 @@ from ..types import (
ProductVariant,
)
from ..utils import (
- attributes_to_json,
validate_attribute_input_for_product,
validate_attribute_input_for_variant,
)
@@ -546,7 +545,11 @@ class AttributeAssignmentMixin:
if not nodes:
raise ValidationError(
- f"Could not resolve to a node: ids={global_ids} and slugs={list(slugs)}"
+ (
+ f"Could not resolve to a node: ids={global_ids}"
+ "and slugs={list(slugs)}"
+ ),
+ code=ProductErrorCode.NOT_FOUND,
)
nodes_pk_list = set()
@@ -557,11 +560,17 @@ class AttributeAssignmentMixin:
for pk, global_id in zip(pks, global_ids):
if pk not in nodes_pk_list:
- raise ValidationError(f"Could not resolve {global_id!r} to Attribute")
+ raise ValidationError(
+ f"Could not resolve {global_id!r} to Attribute",
+ code=ProductErrorCode.NOT_FOUND,
+ )
for slug in slugs:
if slug not in nodes_slug_list:
- raise ValidationError(f"Could not resolve slug {slug!r} to Attribute")
+ raise ValidationError(
+ f"Could not resolve slug {slug!r} to Attribute",
+ code=ProductErrorCode.NOT_FOUND,
+ )
return nodes
@@ -570,9 +579,15 @@ class AttributeAssignmentMixin:
"""Resolve an Attribute global ID into an internal ID (int)."""
graphene_type, internal_id = from_global_id(global_id) # type: str, str
if graphene_type != "Attribute":
- raise ValidationError(f"Must receive an Attribute id, got {graphene_type}.")
+ raise ValidationError(
+ f"Must receive an Attribute id, got {graphene_type}.",
+ code=ProductErrorCode.INVALID,
+ )
if not internal_id.isnumeric():
- raise ValidationError(f"An invalid ID value was passed: {global_id}")
+ raise ValidationError(
+ f"An invalid ID value was passed: {global_id}",
+ code=ProductErrorCode.INVALID,
+ )
return int(internal_id)
@classmethod
@@ -605,7 +620,8 @@ class AttributeAssignmentMixin:
if qs.filter(missing_required_filter).exists():
raise ValidationError(
- "All attributes flagged as having a value required must be supplied."
+ "All attributes flagged as having a value required must be supplied.",
+ code=ProductErrorCode.REQUIRED,
)
@classmethod
@@ -618,7 +634,9 @@ class AttributeAssignmentMixin:
- ensure the values are correct for a variant
"""
if len(cleaned_input) != qs.count():
- raise ValidationError("All attributes must take a value")
+ raise ValidationError(
+ "All attributes must take a value", code=ProductErrorCode.REQUIRED
+ )
for attribute, values in cleaned_input:
validate_attribute_input_for_variant(attribute, values)
@@ -671,7 +689,10 @@ class AttributeAssignmentMixin:
elif slug:
slugs[slug] = values
else:
- raise ValidationError("You must whether supply an ID or a slug")
+ raise ValidationError(
+ "You must whether supply an ID or a slug",
+ code=ProductErrorCode.REQUIRED,
+ )
attributes = cls._resolve_attribute_nodes(
attributes_qs, global_ids=global_ids, pks=pks.keys(), slugs=slugs.keys()
@@ -763,7 +784,7 @@ class ProductCreate(ModelMutation):
attributes, product_type
)
except ValidationError as exc:
- raise ValidationError({"attributes": str(exc.args[0])})
+ raise ValidationError({"attributes": exc})
clean_seo_fields(cleaned_input)
cls.clean_sku(product_type, cleaned_input)
@@ -1043,7 +1064,7 @@ class ProductVariantCreate(ModelMutation):
attributes, product_type
)
except ValidationError as exc:
- raise ValidationError({"attributes": str(exc.args[0])})
+ raise ValidationError({"attributes": exc})
return cleaned_input
@classmethod
diff --git a/saleor/graphql/product/utils.py b/saleor/graphql/product/utils.py
index fd1275021c..b4237a019e 100644
--- a/saleor/graphql/product/utils.py
+++ b/saleor/graphql/product/utils.py
@@ -3,30 +3,45 @@ from typing import List
from django.core.exceptions import ValidationError
from ...product import AttributeInputType, models
+from ...product.error_codes import ProductErrorCode
def validate_attribute_input_for_product(instance: models.Attribute, values: List[str]):
if not values:
if not instance.value_required:
return
- raise ValidationError(f"{instance.slug} expects a value but none were given")
+ raise ValidationError(
+ f"{instance.slug} expects a value but none were given",
+ code=ProductErrorCode.REQUIRED,
+ )
if instance.input_type != AttributeInputType.MULTISELECT and len(values) != 1:
raise ValidationError(
- f"A {instance.input_type} attribute must take only one value"
+ f"A {instance.input_type} attribute must take only one value",
+ code=ProductErrorCode.INVALID,
)
for value in values:
if not value.strip():
- raise ValidationError("Attribute values cannot be blank")
+ raise ValidationError(
+ "Attribute values cannot be blank", code=ProductErrorCode.REQUIRED
+ )
def validate_attribute_input_for_variant(instance: models.Attribute, values: List[str]):
if not values:
- raise ValidationError(f"{instance.slug} expects a value but none were given")
+ raise ValidationError(
+ f"{instance.slug} expects a value but none were given",
+ code=ProductErrorCode.REQUIRED,
+ )
if len(values) != 1:
- raise ValidationError(f"A variant attribute cannot take more than one value")
+ raise ValidationError(
+ f"A variant attribute cannot take more than one value",
+ code=ProductErrorCode.INVALID,
+ )
if not values[0].strip():
- raise ValidationError("Attribute values cannot be blank")
+ raise ValidationError(
+ "Attribute values cannot be blank", code=ProductErrorCode.REQUIRED
+ )
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 79b4bd9934..f768848164 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1812,10 +1812,6 @@ type MenuDelete {
menu: Menu
}
-input MenuFilterInput {
- search: String
-}
-
type MenuError {
field: String
message: String
@@ -1834,6 +1830,10 @@ enum MenuErrorCode {
UNIQUE
}
+input MenuFilterInput {
+ search: String
+}
+
input MenuInput {
name: String
}
@@ -3122,6 +3122,7 @@ type ProductTypeUpdateMeta {
type ProductTypeUpdatePrivateMeta {
errors: [Error!]
+ productErrors: [ProductError!]
productType: ProductType
}
@@ -3201,6 +3202,7 @@ type ProductVariantCountableEdge {
type ProductVariantCreate {
errors: [Error!]
+ productErrors: [ProductError!]
productVariant: ProductVariant
}
diff --git a/tests/api/test_base_mutation.py b/tests/api/test_base_mutation.py
index 8fa31c23b7..ffdc60c0bd 100644
--- a/tests/api/test_base_mutation.py
+++ b/tests/api/test_base_mutation.py
@@ -81,7 +81,7 @@ def test_user_error_nonexistent_id(schema_context):
user_errors = result.data["test"]["errors"]
assert user_errors
assert user_errors[0]["field"] == "productId"
- assert user_errors[0]["message"] == "Couldn't resolve to a node"
+ assert user_errors[0]["message"] == "Couldn't resolve to a node: not-really"
def test_user_error_id_of_different_type(product, schema_context):
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index eb3af03ffa..9e207a8eef 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -1187,7 +1187,7 @@ def test_checkout_complete_invalid_checkout_id(user_api_client):
response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
content = get_graphql_content(response)
data = content["data"]["checkoutComplete"]
- assert data["errors"][0]["message"] == "Couldn't resolve to a node"
+ assert data["errors"][0]["message"] == "Couldn't resolve to a node: invalidId"
assert data["errors"][0]["field"] == "checkoutId"
assert orders_count == Order.objects.count()