Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Simplify ValidationError in attribute
IKarbowiak3fd9659417e0
diff --git a/saleor/graphql/product/mutations/attributes.py b/saleor/graphql/product/mutations/attributes.pyindex 7471067e5f..bd96d35d56 100644--- a/saleor/graphql/product/mutations/attributes.py+++ b/saleor/graphql/product/mutations/attributes.py@@ -171,15 +171,9 @@ class AttributeMixin:cleaned_input = validate_slug_and_generate_if_needed(instance, "name", cleaned_input)- except ValidationError:- raise ValidationError(- {- "slug": ValidationError(- "Slug value cannot be blank.",- code=ProductErrorCode.REQUIRED.value,- )- }- )+ except ValidationError as error:+ error.code = ProductErrorCode.REQUIRED.value+ raise ValidationError({"slug": error})return cleaned_inputdiff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.pyindex f29c4e42c3..28c94005e1 100644--- a/tests/api/test_attributes.py+++ b/tests/api/test_attributes.py@@ -483,9 +483,10 @@ def test_create_attribute_with_given_slug(mutation createAttribute($name: String!, $slug: String) {attributeCreate(input: {name: $name, slug: $slug}) {- errors {+ productErrors {fieldmessage+ code}attribute {slug@@ -498,7 +499,7 @@ def test_create_attribute_with_given_slug(variables = {"name": attribute_name, "slug": input_slug}content = get_graphql_content(staff_api_client.post_graphql(query, variables))- assert not content["data"]["attributeCreate"]["errors"]+ assert not content["data"]["attributeCreate"]["productErrors"]assert content["data"]["attributeCreate"]["attribute"]["slug"] == expected_slug@@ -709,7 +710,7 @@ def test_update_attribute_slug(content = get_graphql_content(response)attribute.refresh_from_db()data = content["data"]["attributeUpdate"]- errors = data["errors"]+ errors = data["productErrors"]if not error_message:assert data["attribute"]["name"] == name == attribute.nameassert data["attribute"]["slug"] == input_slug == attribute.slug@@ -717,7 +718,7 @@ def test_update_attribute_slug(assert errorsassert data["attribute"] is Noneassert errors[0]["field"] == "slug"- assert errors[0]["message"] == error_message+ assert errors[0]["code"] == ProductErrorCode.REQUIRED.namedef test_update_attribute_slug_exists(@@ -744,12 +745,12 @@ def test_update_attribute_slug_exists(content = get_graphql_content(response)attribute.refresh_from_db()data = content["data"]["attributeUpdate"]- errors = data["errors"]+ errors = data["productErrors"]assert errorsassert data["attribute"] is Noneassert errors[0]["field"] == "slug"- assert errors[0]["message"] == "Attribute with this Slug already exists."+ assert errors[0]["code"] == ProductErrorCode.UNIQUE.name@pytest.mark.parametrize(@@ -812,14 +813,14 @@ def test_update_attribute_slug_and_name(content = get_graphql_content(response)attribute.refresh_from_db()data = content["data"]["attributeUpdate"]- errors = data["errors"]+ errors = data["productErrors"]if not error_message:assert data["attribute"]["name"] == input_name == attribute.nameassert data["attribute"]["slug"] == input_slug == attribute.slugelse:assert errorsassert errors[0]["field"] == error_field- assert errors[0]["message"] == error_message+ assert errors[0]["code"] == ProductErrorCode.REQUIRED.name@pytest.mark.parametrize(