Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add missing error codes
Aleksander Spyra5c2b69d6d894
diff --git a/saleor/checkout/forms.py b/saleor/checkout/forms.pyindex 8a8e8977dd..0860f2ab08 100644--- a/saleor/checkout/forms.py+++ b/saleor/checkout/forms.py@@ -155,7 +155,7 @@ class ReplaceCheckoutLineForm(AddToCheckoutForm):self.variant.check_quantity(quantity)except InsufficientStock as e:msg = self.error_messages["insufficient-stock"]- raise forms.ValidationError(msg % e.item.quantity_available)+ raise forms.ValidationError(msg % e.item.quantity_available, code=e.code)return quantitydef clean(self):diff --git a/saleor/core/weight.py b/saleor/core/weight.pyindex 15a75ed94b..ae838aed8f 100644--- a/saleor/core/weight.py+++ b/saleor/core/weight.py@@ -21,6 +21,8 @@ from django.template.loader import render_to_stringfrom django.utils.translation import pgettext_lazyfrom measurement.measures import Weight+from ..graphql.core.utils.error_codes import CommonErrorCode+class WeightUnits:KILOGRAM = "kg"@@ -98,7 +100,8 @@ class WeightField(forms.FloatField):raise Exception("%r is not a valid weight." % (weight,))if weight.unit != unit:raise forms.ValidationError(- "Invalid unit: %r (expected %r)." % (weight.unit, unit)+ "Invalid unit: %r (expected %r)." % (weight.unit, unit),+ code=CommonErrorCode.INVALID_WEIGHT_UNIT,)super().validate(weight.value)diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.pyindex 6801267969..d02a23173a 100644--- a/saleor/graphql/account/mutations/base.py+++ b/saleor/graphql/account/mutations/base.py@@ -157,11 +157,18 @@ class PasswordChange(BaseMutation):new_password = data["new_password"]if not user.check_password(old_password):- raise ValidationError({"old_password": "Old password isn't valid."})+ raise ValidationError(+ {+ "old_password": ValidationError(+ "Old password isn't valid.",+ code=AccountErrorCode.INVALID_PASSWORD,+ )+ }+ )try:password_validation.validate_password(new_password, user)except ValidationError as error:- raise ValidationError({"new_password": error.messages})+ raise ValidationError({"new_password": error})user.set_password(new_password)user.save(update_fields=["password"])diff --git a/saleor/graphql/core/utils/error_codes.py b/saleor/graphql/core/utils/error_codes.pyindex 8a6ee1673e..03fb35c439 100644--- a/saleor/graphql/core/utils/error_codes.py+++ b/saleor/graphql/core/utils/error_codes.py@@ -35,6 +35,8 @@ DJANGO_PASSWORD_VALIDATION_ERROR_CODES = ["password_too_similar",]+DJANGO_MODEL_FIELDS_ERROR_CODES = ["blank", "null", "unique", "unique_for_date"]+class AccountErrorCode(Enum):ACTIVATE_OWN_ACCOUNT = "activate_own_account"@@ -47,6 +49,7 @@ class AccountErrorCode(Enum):DELETE_STUFF_ACCOUNT = "delete_stuff_account"DELETE_SUPERUSER_ACCOUNT = "delete_superuser_account"INVALID_COUNTRY = "invalid_country"+ INVALID_PASSWORD = "invalid_password"INVALID_PHONE_NUMBER = "invalid_phone_number"INVALID_USER_TOKEN = "invalid_user_token"NOT_USERS_ADDRESS = "not_users_address"@@ -103,6 +106,7 @@ class CommonErrorCode(Enum):INCORRECT_VALUE = "incorrect_value"INVALID_FILE_TYPE = "invalid_file_type"INVALID_STOREFRONT_URL = "invalid_storefront_url"+ INVALID_WEIGHT_UNIT = "invalid_weight_unit"MISSING_VALUE = "missing_value"NON_BLANK_VALUE_REQUIRED = "non_blank_value_required"PARTIAL_PAYMENT_NOT_ALLOWED = "partial_payment_not_allowed"@@ -163,6 +167,7 @@ ERROR_CODES = (+ DJANGO_VALIDATORS_ERROR_CODES+ DJANGO_FORM_FIELDS_ERROR_CODES+ DJANGO_PASSWORD_VALIDATION_ERROR_CODES+ + DJANGO_MODEL_FIELDS_ERROR_CODES+ saleor_error_codes)diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.pyindex 22cd819ae2..3c8c66c910 100644--- a/saleor/graphql/payment/mutations.py+++ b/saleor/graphql/payment/mutations.py@@ -179,7 +179,7 @@ class PaymentVoid(BaseMutation):try:gateway_void(payment)except PaymentError as e:- raise ValidationError(str(e), CommonErrorCode.PAYMENT_ERROR)+ raise ValidationError(str(e), code=CommonErrorCode.PAYMENT_ERROR)return PaymentVoid(payment=payment)@@ -200,5 +200,5 @@ class PaymentSecureConfirm(BaseMutation):try:gateway_confirm(payment)except PaymentError as e:- raise ValidationError(str(e))+ raise ValidationError(str(e), code=CommonErrorCode.PAYMENT_ERROR)return PaymentSecureConfirm(payment=payment)diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 421ad9d765..953f531160 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): ProductTypeCountableConnectionprivateMeta: [MetaStore]!meta: [MetaStore]!inputType: AttributeInputTypeEnum@@ -971,6 +971,7 @@ enum CountryCode {LTLUMO+ MKMGMWMY@@ -1004,7 +1005,6 @@ enum CountryCode {NUNFKP- MKMPNOOM@@ -1400,6 +1400,7 @@ enum ErrorCode {ATTRIBUTE_VALUES_NOT_UNIQUEAUTHORIZATION_KEY_ALREADY_EXISTSBILLING_ADDRESS_NOT_SET+ BLANKCANNOT_ASSIGN_NODECANNOT_CANCEL_FULFILLMENTCANNOT_CANCEL_ORDER@@ -1434,12 +1435,14 @@ enum ErrorCode {INVALID_IMAGEINVALID_LISTINVALID_MENU_ITEM+ INVALID_PASSWORDINVALID_PHONE_NUMBERINVALID_PROMO_CODEINVALID_SHIPPING_METHODINVALID_STOREFRONT_URLINVALID_TIMEINVALID_USER_TOKEN+ INVALID_WEIGHT_UNITLIMIT_VALUEMAX_DECIMAL_PLACESMAX_DIGITS@@ -1456,6 +1459,7 @@ enum ErrorCode {NON_BLANK_VALUE_REQUIREDNOT_PRODUCTS_IMAGENOT_USERS_ADDRESS+ NULLNULL_CHARACTERS_NOT_ALLOWEDORDER_INVALID_SHIPPING_METHODORDER_NO_PAYMENT@@ -1482,6 +1486,8 @@ enum ErrorCode {SHIPPING_NOT_REQUIREDTAX_ERRORTOO_MANY_ITEMS+ UNIQUE+ UNIQUE_FOR_DATEUNKNOWNUSER_DOES_NOT_EXISTVALUE_ERRORdiff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.pyindex 2c88fa8507..5ec869adb6 100644--- a/tests/api/test_checkout.py+++ b/tests/api/test_checkout.py@@ -246,6 +246,7 @@ def test_checkout_create_required_email(api_client, variant):assert errorsassert errors[0]["field"] == "email"assert errors[0]["message"] == "This field cannot be blank."+ assert errors[0]["code"] == "BLANK"def test_checkout_create_default_email_for_logged_in_customer(user_api_client, variant):@@ -1008,6 +1009,7 @@ def test_checkout_email_update_validation(user_api_client, checkout_with_item):assert errorsassert errors[0]["field"] == "email"assert errors[0]["message"] == "This field cannot be blank."+ assert errors[0]["code"] == "BLANK"MUTATION_CHECKOUT_COMPLETE = """