Skip to content

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

Add missing typed error codes

Marcin Gębala39d0d8207e2b
diff --git a/saleor/core/error_codes.py b/saleor/core/error_codes.py
index cea4089e3d..220d5ced76 100644
--- a/saleor/core/error_codes.py
+++ b/saleor/core/error_codes.py
@@ -15,3 +15,9 @@ class MetadataErrorCode(Enum):
GRAPHQL_ERROR = "graphql_error"
INVALID = "invalid"
NOT_FOUND = "not_found"
+
+
+class TranslationErrorCode(Enum):
+ GRAPHQL_ERROR = "graphql_error"
+ NOT_FOUND = "not_found"
+ REQUIRED = "required"
diff --git a/saleor/discount/error_codes.py b/saleor/discount/error_codes.py
index ca845d9090..dfa2720473 100644
--- a/saleor/discount/error_codes.py
+++ b/saleor/discount/error_codes.py
@@ -3,6 +3,7 @@ from enum import Enum
class DiscountErrorCode(Enum):
ALREADY_EXISTS = "already_exists"
+ GRAPHQL_ERROR = "graphql_error"
INVALID = "invalid"
NOT_FOUND = "not_found"
REQUIRED = "required"
diff --git a/saleor/graphql/core/enums.py b/saleor/graphql/core/enums.py
index 4532859824..13425bd39b 100644
--- a/saleor/graphql/core/enums.py
+++ b/saleor/graphql/core/enums.py
@@ -101,3 +101,4 @@ StockErrorCode = graphene.Enum.from_enum(warehouse_error_codes.StockErrorCode)
WarehouseErrorCode = graphene.Enum.from_enum(warehouse_error_codes.WarehouseErrorCode)
WebhookErrorCode = graphene.Enum.from_enum(webhook_error_codes.WebhookErrorCode)
WishlistErrorCode = graphene.Enum.from_enum(wishlist_error_codes.WishlistErrorCode)
+TranslationErrorCode = graphene.Enum.from_enum(core_error_codes.TranslationErrorCode)
diff --git a/saleor/graphql/core/mutations.py b/saleor/graphql/core/mutations.py
index 68fb9c8571..4b6ffb580a 100644
--- a/saleor/graphql/core/mutations.py
+++ b/saleor/graphql/core/mutations.py
@@ -590,7 +590,11 @@ class CreateToken(ObtainJSONWebToken):
the mutation works.
"""
- errors = graphene.List(graphene.NonNull(Error), required=True)
+ errors = graphene.List(
+ graphene.NonNull(Error),
+ required=True,
+ deprecation_reason="Use typed errors with error codes.",
+ )
account_errors = graphene.List(
graphene.NonNull(AccountError),
description="List of errors that occurred executing the mutation.",
diff --git a/saleor/graphql/core/types/common.py b/saleor/graphql/core/types/common.py
index e18e452c1a..d2c61de7b0 100644
--- a/saleor/graphql/core/types/common.py
+++ b/saleor/graphql/core/types/common.py
@@ -18,6 +18,7 @@ from ..enums import (
ShippingErrorCode,
ShopErrorCode,
StockErrorCode,
+ TranslationErrorCode,
WarehouseErrorCode,
WebhookErrorCode,
WishlistErrorCode,
@@ -138,6 +139,10 @@ class WishlistError(Error):
code = WishlistErrorCode(description="The error code.", required=True)
+class TranslationError(Error):
+ code = TranslationErrorCode(description="The error code.", required=True)
+
+
class LanguageDisplay(graphene.ObjectType):
code = LanguageCodeEnum(description="Language code.", required=True)
language = graphene.String(description="Language.", required=True)
diff --git a/saleor/graphql/discount/bulk_mutations.py b/saleor/graphql/discount/bulk_mutations.py
index 713a27d349..a16516780e 100644
--- a/saleor/graphql/discount/bulk_mutations.py
+++ b/saleor/graphql/discount/bulk_mutations.py
@@ -3,6 +3,7 @@ import graphene
from ...core.permissions import DiscountPermissions
from ...discount import models
from ..core.mutations import ModelBulkDeleteMutation
+from ..core.types.common import DiscountError
class SaleBulkDelete(ModelBulkDeleteMutation):
@@ -15,6 +16,8 @@ class SaleBulkDelete(ModelBulkDeleteMutation):
description = "Deletes sales."
model = models.Sale
permissions = (DiscountPermissions.MANAGE_DISCOUNTS,)
+ error_type_class = DiscountError
+ error_type_field = "discount_errors"
class VoucherBulkDelete(ModelBulkDeleteMutation):
@@ -27,3 +30,5 @@ class VoucherBulkDelete(ModelBulkDeleteMutation):
description = "Deletes vouchers."
model = models.Voucher
permissions = (DiscountPermissions.MANAGE_DISCOUNTS,)
+ error_type_class = DiscountError
+ error_type_field = "discount_errors"
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 015bc71458..d007ad49b8 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -336,6 +336,7 @@ type AttributeTranslatableContent implements Node {
type AttributeTranslate {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes.")
+ translationErrors: [TranslationError!]!
attribute: Attribute
}
@@ -435,6 +436,7 @@ type AttributeValueTranslatableContent implements Node {
type AttributeValueTranslate {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes.")
+ translationErrors: [TranslationError!]!
attributeValue: AttributeValue
}
@@ -609,6 +611,7 @@ type CategoryTranslatableContent implements Node {
type CategoryTranslate {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes.")
+ translationErrors: [TranslationError!]!
category: Category
}
@@ -999,6 +1002,7 @@ type CollectionTranslatableContent implements Node {
type CollectionTranslate {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes.")
+ translationErrors: [TranslationError!]!
collection: Collection
}
@@ -1323,7 +1327,7 @@ type CountryDisplay {
type CreateToken {
token: String
- errors: [Error!]!
+ errors: [Error!]! @deprecated(reason: "Use typed errors with error codes.")
accountErrors: [AccountError!]!
user: User
}
@@ -1521,6 +1525,7 @@ type DiscountError {
enum DiscountErrorCode {
ALREADY_EXISTS
+ GRAPHQL_ERROR
INVALID
NOT_FOUND
REQUIRED
@@ -2054,6 +2059,7 @@ type MenuItemTranslatableContent implements Node {
type MenuItemTranslate {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes.")
+ translationErrors: [TranslationError!]!
menuItem: MenuItem
}
@@ -2870,6 +2876,7 @@ type PageTranslatableContent implements Node {
type PageTranslate {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes.")
+ translationErrors: [TranslationError!]!
page: PageTranslatableContent
}
@@ -3404,6 +3411,7 @@ type ProductTranslatableContent implements Node {
type ProductTranslate {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes.")
+ translationErrors: [TranslationError!]!
product: Product
}
@@ -3695,6 +3703,7 @@ type ProductVariantTranslatableContent implements Node {
type ProductVariantTranslate {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes.")
+ translationErrors: [TranslationError!]!
productVariant: ProductVariant
}
@@ -3845,6 +3854,7 @@ type SaleAddCatalogues {
type SaleBulkDelete {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes.")
count: Int!
+ discountErrors: [DiscountError!]!
}
type SaleCountableConnection {
@@ -3916,6 +3926,7 @@ type SaleTranslatableContent implements Node {
type SaleTranslate {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes.")
+ translationErrors: [TranslationError!]!
sale: Sale
}
@@ -4048,7 +4059,7 @@ type ServiceAccountUpdatePrivateMeta {
type SetPassword {
token: String
- errors: [Error!]!
+ errors: [Error!]! @deprecated(reason: "Use typed errors with error codes.")
accountErrors: [AccountError!]!
user: User
}
@@ -4134,6 +4145,7 @@ input ShippingPriceInput {
type ShippingPriceTranslate {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes.")
+ translationErrors: [TranslationError!]!
shippingMethod: ShippingMethod
}
@@ -4289,6 +4301,7 @@ input ShopSettingsInput {
type ShopSettingsTranslate {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes.")
shop: Shop
+ translationErrors: [TranslationError!]!
}
input ShopSettingsTranslationInput {
@@ -4554,6 +4567,18 @@ enum TranslatableKinds {
VOUCHER
}
+type TranslationError {
+ field: String
+ message: String
+ code: TranslationErrorCode!
+}
+
+enum TranslationErrorCode {
+ GRAPHQL_ERROR
+ NOT_FOUND
+ REQUIRED
+}
+
input TranslationInput {
seoTitle: String
seoDescription: String
@@ -4745,6 +4770,7 @@ type VoucherAddCatalogues {
type VoucherBulkDelete {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes.")
count: Int!
+ discountErrors: [DiscountError!]!
}
type VoucherCountableConnection {
@@ -4833,6 +4859,7 @@ type VoucherTranslatableContent implements Node {
type VoucherTranslate {
errors: [Error!]! @deprecated(reason: "Use typed errors with error codes.")
+ translationErrors: [TranslationError!]!
voucher: Voucher
}
diff --git a/saleor/graphql/translations/mutations.py b/saleor/graphql/translations/mutations.py
index 9a5f878d19..ae19c4e32f 100644
--- a/saleor/graphql/translations/mutations.py
+++ b/saleor/graphql/translations/mutations.py
@@ -1,4 +1,6 @@
+from enum import Enum
import graphene
+from django.core.exceptions import ValidationError
from ...core.permissions import SitePermissions
from ...discount import models as discount_models
@@ -8,6 +10,7 @@ from ...product import models as product_models
from ...shipping import models as shipping_models
from ..core.mutations import BaseMutation, ModelMutation, registry
from ..shop.types import Shop
+from ..core.types.common import TranslationError
from .enums import LanguageCodeEnum
# discount types need to be imported to get Voucher in the graphene registry
@@ -24,6 +27,11 @@ class BaseTranslateMutation(ModelMutation):
@classmethod
def perform_mutation(cls, _root, info, **data):
+ if "id" in data and not data["id"]:
+ raise ValidationError(
+ {"id": ValidationError("This field is required", code="required")}
+ )
+
model_type = registry.get_type_for_model(cls._meta.model)
instance = cls.get_node_or_error(info, data["id"], only_type=model_type)
instance.translations.update_or_create(
@@ -57,6 +65,8 @@ class CategoryTranslate(BaseTranslateMutation):
class Meta:
description = "Creates/Updates translations for Category."
model = product_models.Category
+ error_type_class = TranslationError
+ error_type_field = "translation_errors"
class ProductTranslate(BaseTranslateMutation):
@@ -70,6 +80,8 @@ class ProductTranslate(BaseTranslateMutation):
class Meta:
description = "Creates/Updates translations for Product."
model = product_models.Product
+ error_type_class = TranslationError
+ error_type_field = "translation_errors"
class CollectionTranslate(BaseTranslateMutation):
@@ -83,6 +95,8 @@ class CollectionTranslate(BaseTranslateMutation):
class Meta:
description = "Creates/Updates translations for collection."
model = product_models.Collection
+ error_type_class = TranslationError
+ error_type_field = "translation_errors"
class ProductVariantTranslate(BaseTranslateMutation):
@@ -96,6 +110,8 @@ class ProductVariantTranslate(BaseTranslateMutation):
class Meta:
description = "Creates/Updates translations for Product Variant."
model = product_models.ProductVariant
+ error_type_class = TranslationError
+ error_type_field = "translation_errors"
class AttributeTranslate(BaseTranslateMutation):
@@ -109,6 +125,8 @@ class AttributeTranslate(BaseTranslateMutation):
class Meta:
description = "Creates/Updates translations for attribute."
model = product_models.Attribute
+ error_type_class = TranslationError
+ error_type_field = "translation_errors"
class AttributeValueTranslate(BaseTranslateMutation):
@@ -122,6 +140,8 @@ class AttributeValueTranslate(BaseTranslateMutation):
class Meta:
description = "Creates/Updates translations for attribute value."
model = product_models.AttributeValue
+ error_type_class = TranslationError
+ error_type_field = "translation_errors"
class SaleTranslate(BaseTranslateMutation):
@@ -135,6 +155,8 @@ class SaleTranslate(BaseTranslateMutation):
class Meta:
description = "Creates/updates translations for a sale."
model = discount_models.Sale
+ error_type_class = TranslationError
+ error_type_field = "translation_errors"
class VoucherTranslate(BaseTranslateMutation):
@@ -148,6 +170,8 @@ class VoucherTranslate(BaseTranslateMutation):
class Meta:
description = "Creates/Updates translations for Voucher."
model = discount_models.Voucher
+ error_type_class = TranslationError
+ error_type_field = "translation_errors"
class ShippingPriceTranslate(BaseTranslateMutation):
@@ -161,6 +185,8 @@ class ShippingPriceTranslate(BaseTranslateMutation):
class Meta:
description = "Creates/Updates translations for shipping method."
model = shipping_models.ShippingMethod
+ error_type_class = TranslationError
+ error_type_field = "translation_errors"
class MenuItemTranslate(BaseTranslateMutation):
@@ -174,6 +200,8 @@ class MenuItemTranslate(BaseTranslateMutation):
class Meta:
description = "Creates/Updates translations for Menu Item."
model = menu_models.MenuItem
+ error_type_class = TranslationError
+ error_type_field = "translation_errors"
class PageTranslationInput(SeoTranslationInput):
@@ -193,6 +221,8 @@ class PageTranslate(BaseTranslateMutation):
class Meta:
description = "Creates/Updates translations for Page."
model = page_models.Page
+ error_type_class = TranslationError
+ error_type_field = "translation_errors"
class ShopSettingsTranslationInput(graphene.InputObjectType):
@@ -215,6 +245,8 @@ class ShopSettingsTranslate(BaseMutation):
class Meta:
description = "Creates/Updates translations for Shop Settings."
permissions = (SitePermissions.MANAGE_TRANSLATIONS,)
+ error_type_class = TranslationError
+ error_type_field = "translation_errors"
@classmethod
def perform_mutation(cls, _root, info, language_code, **data):
diff --git a/saleor/graphql/utils.py b/saleor/graphql/utils.py
index a558c640c9..e567ec9a70 100644
--- a/saleor/graphql/utils.py
+++ b/saleor/graphql/utils.py
@@ -25,13 +25,6 @@ def get_database_id(info, node_id, only_type):
return _id
-def _check_graphene_type(requested_graphene_type, received_type):
- if requested_graphene_type:
- assert str(requested_graphene_type) == received_type, (
- "Must receive an {} id."
- ).format(str(requested_graphene_type))
-
-
def resolve_global_ids_to_primary_keys(ids, graphene_type=None):
pks = []
invalid_ids = []
@@ -47,7 +40,10 @@ def resolve_global_ids_to_primary_keys(ids, graphene_type=None):
invalid_ids.append(graphql_id)
continue
- _check_graphene_type(used_type, node_type)
+ # Raise GraphQL error if ID of a different type was passed
+ if used_type and str(used_type) != str(node_type):
+ raise GraphQLError(f"Must receive {str(used_type)} id: {graphql_id}")
+
used_type = node_type
pks.append(_id)
@@ -61,7 +57,7 @@ def _resolve_graphene_type(type_name):
for _, _type in registry._registry.items():
if _type._meta.name == type_name:
return _type
- raise AssertionError("Could not resolve the type {}".format(type_name))
+ raise GraphQLError("Could not resolve the type {}".format(type_name))
def get_nodes(
diff --git a/saleor/site/error_codes.py b/saleor/site/error_codes.py
index ddf78d8225..e5c26058d0 100644
--- a/saleor/site/error_codes.py
+++ b/saleor/site/error_codes.py
@@ -3,3 +3,4 @@ from enum import Enum
class SiteErrorCode(Enum):
FORBIDDEN_CHARACTER = "forbidden_character"
+ GRAPHQL_ERROR = "graphql_error"