Skip to content

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

Initial implementation for Django errors only

Aleksander Spyraf7f30fb91ac7
diff --git a/saleor/graphql/core/enums.py b/saleor/graphql/core/enums.py
index 343ca6f547..5bcdc0157d 100644
--- a/saleor/graphql/core/enums.py
+++ b/saleor/graphql/core/enums.py
@@ -4,6 +4,7 @@ from ...core.permissions import MODELS_PERMISSIONS
from ...core.weight import WeightUnits
from ...extensions.plugins.vatlayer import TaxRateType as CoreTaxRateType
from .utils import str_to_enum
+from .utils.error_codes import ERROR_CODES
# FIXME CoreTaxRateType should be removed after we will drop old api fields dedicated
# to taxes
@@ -58,3 +59,7 @@ PermissionEnum = graphene.Enum(
WeightUnitsEnum = graphene.Enum(
"WeightUnitsEnum", [(str_to_enum(unit[0]), unit[0]) for unit in WeightUnits.CHOICES]
)
+
+ErrorCode = graphene.Enum(
+ "ErrorCode", [(str_to_enum(code), code) for code in ERROR_CODES]
+)
diff --git a/saleor/graphql/core/mutations.py b/saleor/graphql/core/mutations.py
index a15405eb1f..05c0904a18 100644
--- a/saleor/graphql/core/mutations.py
+++ b/saleor/graphql/core/mutations.py
@@ -21,6 +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 get_error_code_from_error
registry = get_global_registry()
@@ -47,16 +48,24 @@ def validation_error_to_error_type(validation_error: ValidationError) -> list:
err_list = []
if hasattr(validation_error, "error_dict"):
# convert field errors
- for field, field_errors in validation_error.message_dict.items():
+ for field, field_errors in validation_error.error_dict.items():
for err in field_errors:
field = (
None if field == NON_FIELD_ERRORS else snake_to_camel_case(field)
)
- err_list.append(Error(field=field, message=err))
+ err_list.append(
+ Error(
+ field=field,
+ message=err.messages[0],
+ code=get_error_code_from_error(err),
+ )
+ )
else:
# convert non-field errors
for err in validation_error.error_list:
- err_list.append(Error(message=err.message))
+ err_list.append(
+ Error(message=err.message, code=get_error_code_from_error(err))
+ )
return err_list
diff --git a/saleor/graphql/core/types/common.py b/saleor/graphql/core/types/common.py
index fd4f23b530..2dfad7107f 100644
--- a/saleor/graphql/core/types/common.py
+++ b/saleor/graphql/core/types/common.py
@@ -2,7 +2,7 @@ import graphene
from ....product.templatetags.product_images import get_thumbnail
from ...translations.enums import LanguageCodeEnum
-from ..enums import PermissionEnum
+from ..enums import ErrorCode, PermissionEnum
from .money import VAT
@@ -20,6 +20,7 @@ class Error(graphene.ObjectType):
required=False,
)
message = graphene.String(description="The error message.")
+ code = ErrorCode(description="The error code.")
class Meta:
description = "Represents an error in the input of a mutation."
diff --git a/saleor/graphql/core/utils/error_codes.py b/saleor/graphql/core/utils/error_codes.py
new file mode 100644
index 0000000000..f812f76334
--- /dev/null
+++ b/saleor/graphql/core/utils/error_codes.py
@@ -0,0 +1,42 @@
+DJANGO_VALIDATORS_ERROR_CODES = [
+ "invalid",
+ "limit_value",
+ "max_value",
+ "min_value",
+ "min_length",
+ "max_length",
+ "max_digits",
+ "max_decimal_places",
+ "max_whole_digits",
+ "invalid_extension",
+ "null_characters_not_allowed",
+]
+
+DJANGO_FORM_FIELDS_ERROR_CODES = [
+ "overflow",
+ "missing",
+ "empty",
+ "contradiction",
+ "invalid_image",
+ "invalid_choice",
+ "invalid_list",
+ "incomplete",
+ "invalid_date",
+ "invalid_time",
+]
+
+ERROR_CODE_UNKNOWN = "unknown"
+
+ERROR_CODES = (
+ [ERROR_CODE_UNKNOWN]
+ + DJANGO_VALIDATORS_ERROR_CODES
+ + DJANGO_FORM_FIELDS_ERROR_CODES
+)
+
+
+def get_error_code_from_error(error):
+ """ """
+ code = error.code
+ if code not in ERROR_CODES:
+ return ERROR_CODE_UNKNOWN
+ return code
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 8c8d128f4d..174861a6ed 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1382,6 +1382,32 @@ type DraftOrderUpdate {
type Error {
field: String
message: String
+ code: ErrorCode
+}
+
+enum ErrorCode {
+ UNKNOWN
+ INVALID
+ LIMIT_VALUE
+ MAX_VALUE
+ MIN_VALUE
+ MIN_LENGTH
+ MAX_LENGTH
+ MAX_DIGITS
+ MAX_DECIMAL_PLACES
+ MAX_WHOLE_DIGITS
+ INVALID_EXTENSION
+ NULL_CHARACTERS_NOT_ALLOWED
+ OVERFLOW
+ MISSING
+ EMPTY
+ CONTRADICTION
+ INVALID_IMAGE
+ INVALID_CHOICE
+ INVALID_LIST
+ INCOMPLETE
+ INVALID_DATE
+ INVALID_TIME
}
type Fulfillment implements Node {