Skip to content

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

Add missing error codes

Aleksander Spyra5c2b69d6d894
diff --git a/saleor/checkout/forms.py b/saleor/checkout/forms.py
index 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 quantity
def clean(self):
diff --git a/saleor/core/weight.py b/saleor/core/weight.py
index 15a75ed94b..ae838aed8f 100644
--- a/saleor/core/weight.py
+++ b/saleor/core/weight.py
@@ -21,6 +21,8 @@ from django.template.loader import render_to_string
from django.utils.translation import pgettext_lazy
from 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.py
index 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.py
index 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.py
index 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.graphql
index 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): ProductTypeCountableConnection
privateMeta: [MetaStore]!
meta: [MetaStore]!
inputType: AttributeInputTypeEnum
@@ -971,6 +971,7 @@ enum CountryCode {
LT
LU
MO
+ MK
MG
MW
MY
@@ -1004,7 +1005,6 @@ enum CountryCode {
NU
NF
KP
- MK
MP
NO
OM
@@ -1400,6 +1400,7 @@ enum ErrorCode {
ATTRIBUTE_VALUES_NOT_UNIQUE
AUTHORIZATION_KEY_ALREADY_EXISTS
BILLING_ADDRESS_NOT_SET
+ BLANK
CANNOT_ASSIGN_NODE
CANNOT_CANCEL_FULFILLMENT
CANNOT_CANCEL_ORDER
@@ -1434,12 +1435,14 @@ enum ErrorCode {
INVALID_IMAGE
INVALID_LIST
INVALID_MENU_ITEM
+ INVALID_PASSWORD
INVALID_PHONE_NUMBER
INVALID_PROMO_CODE
INVALID_SHIPPING_METHOD
INVALID_STOREFRONT_URL
INVALID_TIME
INVALID_USER_TOKEN
+ INVALID_WEIGHT_UNIT
LIMIT_VALUE
MAX_DECIMAL_PLACES
MAX_DIGITS
@@ -1456,6 +1459,7 @@ enum ErrorCode {
NON_BLANK_VALUE_REQUIRED
NOT_PRODUCTS_IMAGE
NOT_USERS_ADDRESS
+ NULL
NULL_CHARACTERS_NOT_ALLOWED
ORDER_INVALID_SHIPPING_METHOD
ORDER_NO_PAYMENT
@@ -1482,6 +1486,8 @@ enum ErrorCode {
SHIPPING_NOT_REQUIRED
TAX_ERROR
TOO_MANY_ITEMS
+ UNIQUE
+ UNIQUE_FOR_DATE
UNKNOWN
USER_DOES_NOT_EXIST
VALUE_ERROR
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index 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 errors
assert 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 errors
assert errors[0]["field"] == "email"
assert errors[0]["message"] == "This field cannot be blank."
+ assert errors[0]["code"] == "BLANK"
MUTATION_CHECKOUT_COMPLETE = """