Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Refactor typed errors implementation
Aleksander Spyra4a48f7ce0a43
diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.pyindex fe08825ebb..3cd62c1d5c 100644--- a/saleor/graphql/account/mutations/base.py+++ b/saleor/graphql/account/mutations/base.py@@ -46,16 +46,8 @@ class AccountErrorMixin:graphene.NonNull(AccountError),description="List of errors that occurred executing the mutation.",)-- @classmethod- def handle_typed_errors(cls, errors: list, **extra):- account_errors = [- AccountError(field=e.field, message=e.message, code=code)- for e, code in errors- ]- return cls(- errors=[e[0] for e in errors], account_errors=account_errors, **extra- )+ ERROR_TYPE_CLASS = AccountError+ ERROR_TYPE_FIELD = "account_errors"class BaseAccountMutation(AccountErrorMixin, BaseMutation):@@ -63,7 +55,7 @@ class BaseAccountMutation(AccountErrorMixin, BaseMutation):abstract = True-class SetPassword(AccountErrorMixin, CreateToken):+class SetPassword(CreateToken, AccountErrorMixin):user = graphene.Field(User, description="An user instance with new password.")class Arguments:@@ -115,6 +107,14 @@ class SetPassword(AccountErrorMixin, CreateToken):user.save(update_fields=["password"])account_events.customer_password_reset_event(user=user)+ @classmethod+ def handle_typed_errors(cls, errors: list):+ account_errors = [+ AccountError(field=e.field, message=e.message, code=code)+ for e, code in errors+ ]+ return cls(errors=[e[0] for e in errors], account_errors=account_errors)+class RequestPasswordReset(BaseAccountMutation):class Arguments:diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.pyindex faf09fe658..ce324fce7f 100644--- a/saleor/graphql/checkout/mutations.py+++ b/saleor/graphql/checkout/mutations.py@@ -56,16 +56,8 @@ class CheckoutErrorMixin:graphene.NonNull(CheckoutError),description="List of errors that occurred executing the mutation.",)-- @classmethod- def handle_typed_errors(cls, errors: list, **extra):- checkout_errors = [- CheckoutError(field=e.field, message=e.message, code=code)- for e, code in errors- ]- return cls(- errors=[e[0] for e in errors], checkout_errors=checkout_errors, **extra- )+ ERROR_TYPE_CLASS = CheckoutError+ ERROR_TYPE_FIELD = "checkout_errors"class BaseCheckoutMutation(CheckoutErrorMixin, BaseMutation):diff --git a/saleor/graphql/core/mutations.py b/saleor/graphql/core/mutations.pyindex 82b123d2f0..17b0aa56fd 100644--- a/saleor/graphql/core/mutations.py+++ b/saleor/graphql/core/mutations.py@@ -262,10 +262,13 @@ class BaseMutation(graphene.Mutation):@classmethoddef handle_typed_errors(cls, errors: list, **extra):- """Return class instance with errors.-- Should be overridden in mutation that supports typed errors.- """+ """Return class instance with errors."""+ if hasattr(cls, "ERROR_TYPE_CLASS") and hasattr(cls, "ERROR_TYPE_FIELD"):+ typed_errors = [+ cls.ERROR_TYPE_CLASS(field=e.field, message=e.message, code=code)+ for e, code in errors+ ]+ extra.update({cls.ERROR_TYPE_FIELD: typed_errors})return cls(errors=[e[0] for e in errors], **extra)diff --git a/saleor/graphql/extensions/mutations.py b/saleor/graphql/extensions/mutations.pyindex f401142a7c..d46ab1aac0 100644--- a/saleor/graphql/extensions/mutations.py+++ b/saleor/graphql/extensions/mutations.py@@ -11,16 +11,8 @@ class ExtensionsErrorMixin:graphene.NonNull(ExtensionsError),description="List of errors that occurred executing the mutation.",)-- @classmethod- def handle_typed_errors(cls, errors: list, **extra):- extensions_errors = [- ExtensionsError(field=e.field, message=e.message, code=code)- for e, code in errors- ]- return cls(- errors=[e[0] for e in errors], extensions_errors=extensions_errors, **extra- )+ ERROR_TYPE_CLASS = ExtensionsError+ ERROR_TYPE_FIELD = "extensions_errors"class ConfigurationItemInput(graphene.InputObjectType):diff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.pyindex b58683d8c3..be6afae9b7 100644--- a/saleor/graphql/giftcard/mutations.py+++ b/saleor/graphql/giftcard/mutations.py@@ -21,16 +21,8 @@ class GiftcardErrorMixin:graphene.NonNull(GiftcardError),description="List of errors that occurred executing the mutation.",)-- @classmethod- def handle_typed_errors(cls, errors: list, **extra):- gift_card_errors = [- GiftcardError(field=e.field, message=e.message, code=code)- for e, code in errors- ]- return cls(- errors=[e[0] for e in errors], gift_card_errors=gift_card_errors, **extra- )+ ERROR_TYPE_CLASS = GiftcardError+ ERROR_TYPE_FIELD = "gift_card_errors"class BaseGiftcardMutation(GiftcardErrorMixin, BaseMutation):diff --git a/saleor/graphql/menu/mutations.py b/saleor/graphql/menu/mutations.pyindex 3af870b590..74ec4fff7c 100644--- a/saleor/graphql/menu/mutations.py+++ b/saleor/graphql/menu/mutations.py@@ -24,13 +24,8 @@ class MenuErrorMixin:graphene.NonNull(MenuError),description="List of errors that occurred executing the mutation.",)-- @classmethod- def handle_typed_errors(cls, errors: list, **extra):- menu_errors = [- MenuError(field=e.field, message=e.message, code=code) for e, code in errors- ]- return cls(errors=[e[0] for e in errors], menu_errors=menu_errors, **extra)+ ERROR_TYPE_CLASS = MenuError+ ERROR_TYPE_FIELD = "menu_errors"class BaseMenuMutation(MenuErrorMixin, BaseMutation):diff --git a/saleor/graphql/order/mutations/base.py b/saleor/graphql/order/mutations/base.pyindex 1f9341c450..72fff6b0dc 100644--- a/saleor/graphql/order/mutations/base.py+++ b/saleor/graphql/order/mutations/base.py@@ -9,14 +9,8 @@ class OrderErrorMixin:graphene.NonNull(OrderError),description="List of errors that occurred executing the mutation.",)-- @classmethod- def handle_typed_errors(cls, errors: list, **extra):- order_errors = [- OrderError(field=e.field, message=e.message, code=code)- for e, code in errors- ]- return cls(errors=[e[0] for e in errors], order_errors=order_errors, **extra)+ ERROR_TYPE_CLASS = OrderError+ ERROR_TYPE_FIELD = "order_errors"class BaseOrderMutation(OrderErrorMixin, BaseMutation):diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.pyindex d86bd6a2f3..f21309ff3f 100644--- a/saleor/graphql/payment/mutations.py+++ b/saleor/graphql/payment/mutations.py@@ -29,16 +29,8 @@ class PaymentErrorMixin:graphene.NonNull(common_types.PaymentError),description="List of errors that occurred executing the mutation.",)-- @classmethod- def handle_typed_errors(cls, errors: list, **extra):- payment_errors = [- common_types.PaymentError(field=e.field, message=e.message, code=code)- for e, code in errors- ]- return cls(- errors=[e[0] for e in errors], payment_errors=payment_errors, **extra- )+ ERROR_TYPE_CLASS = common_types.PaymentError+ ERROR_TYPE_FIELD = "payment_errors"class BasePaymentMutation(PaymentErrorMixin, BaseMutation):diff --git a/saleor/graphql/product/mutations/base.py b/saleor/graphql/product/mutations/base.pyindex f140a7fc94..56090cb325 100644--- a/saleor/graphql/product/mutations/base.py+++ b/saleor/graphql/product/mutations/base.py@@ -9,16 +9,8 @@ class ProductErrorMixin:graphene.NonNull(ProductError),description="List of errors that occurred executing the mutation.",)-- @classmethod- def handle_typed_errors(cls, errors: list, **extra):- product_errors = [- ProductError(field=e.field, message=e.message, code=code)- for e, code in errors- ]- return cls(- errors=[e[0] for e in errors], product_errors=product_errors, **extra- )+ ERROR_TYPE_CLASS = ProductError+ ERROR_TYPE_FIELD = "product_errors"class BaseProductMutation(ProductErrorMixin, BaseMutation):diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex ccce35f5a5..b7076e337d 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -3506,10 +3506,10 @@ type ServiceAccountUpdatePrivateMeta {}type SetPassword {+ accountErrors: [AccountError!]token: Stringerrors: [Error]!user: User- accountErrors: [AccountError!]}type ShippingError {diff --git a/saleor/graphql/shipping/mutations.py b/saleor/graphql/shipping/mutations.pyindex ffbad016f7..587153814c 100644--- a/saleor/graphql/shipping/mutations.py+++ b/saleor/graphql/shipping/mutations.py@@ -16,16 +16,8 @@ class ShippingErrorMixin:graphene.NonNull(ShippingError),description="List of errors that occurred executing the mutation.",)-- @classmethod- def handle_typed_errors(cls, errors: list, **extra):- shipping_errors = [- ShippingError(field=e.field, message=e.message, code=code)- for e, code in errors- ]- return cls(- errors=[e[0] for e in errors], shipping_errors=shipping_errors, **extra- )+ ERROR_TYPE_CLASS = ShippingError+ ERROR_TYPE_FIELD = "shipping_errors"class BaseShippingMutation(ShippingErrorMixin, BaseMutation):diff --git a/saleor/graphql/shop/mutations.py b/saleor/graphql/shop/mutations.pyindex 5a8824ae0b..a39838a682 100644--- a/saleor/graphql/shop/mutations.py+++ b/saleor/graphql/shop/mutations.py@@ -20,13 +20,8 @@ class ShopErrorMixin:graphene.NonNull(ShopError),description="List of errors that occurred executing the mutation.",)-- @classmethod- def handle_typed_errors(cls, errors: list, **extra):- shop_errors = [- ShopError(field=e.field, message=e.message, code=code) for e, code in errors- ]- return cls(errors=[e[0] for e in errors], shop_errors=shop_errors, **extra)+ ERROR_TYPE_CLASS = ShopError+ ERROR_TYPE_FIELD = "shop_errors"class BaseShopMutation(ShopErrorMixin, BaseMutation):