Skip to content

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

Unify sth-already-exists codes

Aleksander Spyra8323acee3530
diff --git a/saleor/core/utils/promo_code.py b/saleor/core/utils/promo_code.py
index 044893c36c..0787855b0d 100644
--- a/saleor/core/utils/promo_code.py
+++ b/saleor/core/utils/promo_code.py
@@ -4,7 +4,7 @@ from django.core.exceptions import ValidationError
from ...discount.models import Voucher
from ...giftcard.models import GiftCard
-from ...graphql.core.utils.error_codes import CheckoutErrorCode
+from ...graphql.core.utils.error_codes import CheckoutErrorCode, CommonErrorCode
class InvalidPromoCode(ValidationError):
@@ -24,7 +24,7 @@ class PromoCodeAlreadyExists(ValidationError):
message = {
"promo_code": ValidationError(
"Promo code already exists.",
- code=CheckoutErrorCode.PROMO_CODE_ALREADY_EXISTS,
+ code=CommonErrorCode.OBJECT_ALREADY_EXISTS,
)
}
super().__init__(message, **kwargs)
diff --git a/saleor/graphql/core/utils/error_codes.py b/saleor/graphql/core/utils/error_codes.py
index 03fb35c439..07dc1fd5f3 100644
--- a/saleor/graphql/core/utils/error_codes.py
+++ b/saleor/graphql/core/utils/error_codes.py
@@ -62,7 +62,6 @@ class CheckoutErrorCode(Enum):
INSUFFICIENT_STOCK = "insufficient_stock"
INVALID_PROMO_CODE = "invalid_promo_code"
INVALID_SHIPPING_METHOD = "invalid_shipping_method"
- PROMO_CODE_ALREADY_EXISTS = "promo_code_already_exists"
QUANTITY_GREATER_THAN_LIMIT = "quantity_greater_than_limit"
SHIPPING_ADDRESS_NOT_SET = "shipping_address_not_set"
SHIPPING_METHOD_NOT_APPLICABLE = "shipping_method_not_applicable"
@@ -99,7 +98,6 @@ class OrderErrorCode(Enum):
class CommonErrorCode(Enum):
- AUTHORIZATION_KEY_ALREADY_EXISTS = "authorization_key_already_exists"
CANNOT_FETCH_TAX_RATES = "cannot_fetch_tax_rates"
DOES_NOT_EXIST = "does_not_exist"
GRAPHQL_ERROR = "graphql_error"
@@ -109,6 +107,7 @@ class CommonErrorCode(Enum):
INVALID_WEIGHT_UNIT = "invalid_weight_unit"
MISSING_VALUE = "missing_value"
NON_BLANK_VALUE_REQUIRED = "non_blank_value_required"
+ OBJECT_ALREADY_EXISTS = "object_already_exists"
PARTIAL_PAYMENT_NOT_ALLOWED = "partial_payment_not_allowed"
PAYMENT_ERROR = "payment_error"
POSITIVE_NUMBER_REQUIRED = "positive_number_required"
@@ -118,12 +117,10 @@ class CommonErrorCode(Enum):
class AttributeErrorCode(Enum):
ATTRIBUTE_ALREADY_ASSIGNED = "attribute_already_assigned"
- ATTRIBUTE_ALREADY_EXISTS = "attribute_already_exists"
ATTRIBUTE_BAD_VALUE = "attribute_bad_value"
ATTRIBUTE_CANNOT_BE_ASSIGNED = "attribute_cannot_be_assigned"
ATTRIBUTE_DISABLED_VARIANTS = "attribute_disabled_variants"
ATTRIBUTE_NON_ASSIGNABLE = "attribute_non_assignable"
- ATTRIBUTE_SLUG_ALREADY_EXISTS = "attribute_slug_already_exists"
ATTRIBUTE_SLUG_BLANK = "attribute_slug_blank"
ATTRIBUTE_VALUES_NOT_UNIQUE = "attribute_values_not_unique"
@@ -135,11 +132,9 @@ class DigitalContentErrorCode(Enum):
class ProductErrorCode(Enum):
NOT_PRODUCTS_IMAGE = "not_products_image"
- PRODUCT_ALREADY_EXISTS = "product_already_exists"
class ShippingErrorCode(Enum):
- DEFAULT_SHIPPING_ZONE_ALREADY_EXISTS = "default_shipping_zone_already_exists"
MAX_LESS_THAN_MIN = "max_less_than_min"
diff --git a/saleor/graphql/product/mutations/attributes.py b/saleor/graphql/product/mutations/attributes.py
index 4e7c7602e8..cdbc8ef85b 100644
--- a/saleor/graphql/product/mutations/attributes.py
+++ b/saleor/graphql/product/mutations/attributes.py
@@ -120,7 +120,7 @@ class AttributeMixin:
raise ValidationError(
{
cls.ATTRIBUTE_VALUES_FIELD: ValidationError(
- msg, code=AttributeErrorCode.ATTRIBUTE_ALREADY_EXISTS
+ msg, code=CommonErrorCode.OBJECT_ALREADY_EXISTS
)
}
)
@@ -186,7 +186,7 @@ class AttributeMixin:
{
"slug": ValidationError(
"This attribute's slug already exists.",
- code=AttributeErrorCode.ATTRIBUTE_SLUG_ALREADY_EXISTS,
+ code=CommonErrorCode.OBJECT_ALREADY_EXISTS,
)
}
)
@@ -545,7 +545,7 @@ def validate_value_is_unique(attribute: models.Attribute, value: models.Attribut
{
"name": ValidationError(
f"Value with slug {value.slug} already exists.",
- code=AttributeErrorCode.ATTRIBUTE_SLUG_ALREADY_EXISTS,
+ code=CommonErrorCode.OBJECT_ALREADY_EXISTS,
)
}
)
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 1b8b5cd604..dc3b293e53 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -756,7 +756,7 @@ class ProductCreate(ModelMutation):
{
"sku": ValidationError(
"Product with this SKU already exists.",
- code=ProductErrorCode.PRODUCT_ALREADY_EXISTS,
+ code=CommonErrorCode.OBJECT_ALREADY_EXISTS,
)
}
)
@@ -832,7 +832,7 @@ class ProductUpdate(ProductCreate):
{
"sku": ValidationError(
"Product with this SKU already exists.",
- code=ProductErrorCode.PRODUCT_ALREADY_EXISTS,
+ code=CommonErrorCode.OBJECT_ALREADY_EXISTS,
)
}
)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 953f531160..ebf8cb255e 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -143,8 +143,8 @@ type AssignNavigation {
type Attribute implements Node {
id: ID!
- productTypes(before: String, after: String, first: Int, last: Int): ProductTypeCountableConnection
- productVariantTypes(before: String, after: String, first: Int, last: Int): ProductTypeCountableConnection
+ productTypes(before: String, after: String, first: Int, last: Int): ProductTypeCountableConnection!
+ productVariantTypes(before: String, after: String, first: Int, last: Int): ProductTypeCountableConnection!
privateMeta: [MetaStore]!
meta: [MetaStore]!
inputType: AttributeInputTypeEnum
@@ -971,7 +971,6 @@ enum CountryCode {
LT
LU
MO
- MK
MG
MW
MY
@@ -1005,6 +1004,7 @@ enum CountryCode {
NU
NF
KP
+ MK
MP
NO
OM
@@ -1390,15 +1390,12 @@ enum ErrorCode {
ACTIVATE_SUPERUSER_ACCOUNT
ASSIGN_MENU_ITEM_TO_ITSELF
ATTRIBUTE_ALREADY_ASSIGNED
- ATTRIBUTE_ALREADY_EXISTS
ATTRIBUTE_BAD_VALUE
ATTRIBUTE_CANNOT_BE_ASSIGNED
ATTRIBUTE_DISABLED_VARIANTS
ATTRIBUTE_NON_ASSIGNABLE
- ATTRIBUTE_SLUG_ALREADY_EXISTS
ATTRIBUTE_SLUG_BLANK
ATTRIBUTE_VALUES_NOT_UNIQUE
- AUTHORIZATION_KEY_ALREADY_EXISTS
BILLING_ADDRESS_NOT_SET
BLANK
CANNOT_ASSIGN_NODE
@@ -1410,7 +1407,6 @@ enum ErrorCode {
CONTRADICTION
DEACTIVATE_OWN_ACCOUNT
DEACTIVATE_SUPERUSER_ACCOUNT
- DEFAULT_SHIPPING_ZONE_ALREADY_EXISTS
DELETE_LINE_NON_DRAFT_ORDER
DELETE_NON_DRAFT_ORDER
DELETE_NON_STAFF_USER
@@ -1461,6 +1457,7 @@ enum ErrorCode {
NOT_USERS_ADDRESS
NULL
NULL_CHARACTERS_NOT_ALLOWED
+ OBJECT_ALREADY_EXISTS
ORDER_INVALID_SHIPPING_METHOD
ORDER_NO_PAYMENT
ORDER_NO_SHIPPING_ADDRESS
@@ -1473,8 +1470,6 @@ enum ErrorCode {
PASSWORD_TOO_SIMILAR
PAYMENT_ERROR
POSITIVE_NUMBER_REQUIRED
- PRODUCT_ALREADY_EXISTS
- PROMO_CODE_ALREADY_EXISTS
QUANTITY_GREATER_THAN_LIMIT
QUANTITY_LESS_THAN_ONE
REFUND_MANUAL_PAYMENT
diff --git a/saleor/graphql/shipping/mutations.py b/saleor/graphql/shipping/mutations.py
index 08d341491e..f14afab9d3 100644
--- a/saleor/graphql/shipping/mutations.py
+++ b/saleor/graphql/shipping/mutations.py
@@ -5,7 +5,7 @@ from ...dashboard.shipping.forms import default_shipping_zone_exists
from ...shipping import models
from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ..core.scalars import Decimal, WeightScalar
-from ..core.utils.error_codes import ShippingErrorCode
+from ..core.utils.error_codes import CommonErrorCode, ShippingErrorCode
from .enums import ShippingMethodTypeEnum
from .types import ShippingMethod, ShippingZone
@@ -56,7 +56,7 @@ class ShippingZoneMixin:
{
"default": ValidationError(
"Default shipping zone already exists.",
- code=ShippingErrorCode.DEFAULT_SHIPPING_ZONE_ALREADY_EXISTS,
+ code=CommonErrorCode.OBJECT_ALREADY_EXISTS,
)
}
)
diff --git a/saleor/graphql/shop/mutations.py b/saleor/graphql/shop/mutations.py
index ce2bf3ba23..d0a71bc0d9 100644
--- a/saleor/graphql/shop/mutations.py
+++ b/saleor/graphql/shop/mutations.py
@@ -191,7 +191,7 @@ class AuthorizationKeyAdd(BaseMutation):
{
"key_type": ValidationError(
"Authorization key already exists.",
- code=CommonErrorCode.AUTHORIZATION_KEY_ALREADY_EXISTS,
+ code=CommonErrorCode.OBJECT_ALREADY_EXISTS,
)
}
)
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index 95fe9ce6e0..14fdcbdbae 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -1656,7 +1656,7 @@ def test_set_password_invalid_email(user_api_client):
errors = content["data"]["setPassword"]["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "email"
- assert errors[0]["code"] == AccountErrorCode.NOT_USERS_ADDRESS.name
+ assert errors[0]["code"] == AccountErrorCode.USER_DOES_NOT_EXIST.name
def test_set_password_invalid_password(user_api_client, customer_user, settings):
diff --git a/tests/api/test_discount.py b/tests/api/test_discount.py
index 79abf1cc50..b702f753ee 100644
--- a/tests/api/test_discount.py
+++ b/tests/api/test_discount.py
@@ -8,7 +8,7 @@ from django_countries import countries
from saleor.discount import DiscountValueType, VoucherType
from saleor.discount.models import Sale, Voucher
-from saleor.graphql.core.utils.error_codes import CheckoutErrorCode
+from saleor.graphql.core.utils.error_codes import CommonErrorCode
from saleor.graphql.discount.enums import DiscountValueTypeEnum, VoucherTypeEnum
from tests.api.utils import get_graphql_content
@@ -265,7 +265,7 @@ def test_create_voucher_with_existing_gift_card_code(
errors = content["data"]["voucherCreate"]["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "promoCode"
- assert errors[0]["code"] == CheckoutErrorCode.PROMO_CODE_ALREADY_EXISTS.name
+ assert errors[0]["code"] == CommonErrorCode.OBJECT_ALREADY_EXISTS.name
def test_create_voucher_with_existing_voucher_code(
@@ -292,7 +292,7 @@ def test_create_voucher_with_existing_voucher_code(
errors = content["data"]["voucherCreate"]["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "promoCode"
- assert errors[0]["code"] == CheckoutErrorCode.PROMO_CODE_ALREADY_EXISTS.name
+ assert errors[0]["code"] == CommonErrorCode.OBJECT_ALREADY_EXISTS.name
def test_update_voucher(staff_api_client, voucher, permission_manage_discounts):
diff --git a/tests/api/test_gift_card.py b/tests/api/test_gift_card.py
index b366c458b8..de5e63bcc1 100644
--- a/tests/api/test_gift_card.py
+++ b/tests/api/test_gift_card.py
@@ -2,7 +2,7 @@ from datetime import date
import graphene
-from saleor.graphql.core.utils.error_codes import AccountErrorCode, CheckoutErrorCode
+from saleor.graphql.core.utils.error_codes import AccountErrorCode, CommonErrorCode
from tests.api.utils import get_graphql_content
from .utils import assert_no_permission
@@ -284,7 +284,7 @@ def test_create_gift_card_with_existing_voucher_code(
errors = content["data"]["giftCardCreate"]["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "promoCode"
- assert errors[0]["code"] == CheckoutErrorCode.PROMO_CODE_ALREADY_EXISTS.name
+ assert errors[0]["code"] == CommonErrorCode.OBJECT_ALREADY_EXISTS.name
def test_create_gift_card_with_existing_gift_card_code(
@@ -300,7 +300,7 @@ def test_create_gift_card_with_existing_gift_card_code(
errors = content["data"]["giftCardCreate"]["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "promoCode"
- assert errors[0]["code"] == CheckoutErrorCode.PROMO_CODE_ALREADY_EXISTS.name
+ assert errors[0]["code"] == CommonErrorCode.OBJECT_ALREADY_EXISTS.name
def test_create_gift_card_without_user(staff_api_client, permission_manage_gift_card):
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index e236114c81..b388f53f0a 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -1625,7 +1625,7 @@ def test_try_payment_action_generates_event(order, staff_user, payment_dummy):
order=order, user=staff_user, payment=payment_dummy, func=_test_operation
)
- assert exc.value.args[0] == {"payment": message}
+ assert exc.value.args[0]["payment"].message == message
error_event = OrderEvent.objects.get() # type: OrderEvent
assert error_event.type == order_events.OrderEvents.PAYMENT_FAILED