Skip to content

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

Add missing typed error codes

Aleksander Spyra69981d06eeae
diff --git a/saleor/extensions/plugins/avatax/plugin.py b/saleor/extensions/plugins/avatax/plugin.py
index dacda40383..34090ffabb 100644
--- a/saleor/extensions/plugins/avatax/plugin.py
+++ b/saleor/extensions/plugins/avatax/plugin.py
@@ -9,7 +9,7 @@ from django.utils.translation import pgettext_lazy
from prices import Money, TaxedMoney, TaxedMoneyRange
from ....core.taxes import TaxError, TaxType, zero_taxed_money
-from ....graphql.core.utils.error_codes import CommonErrorCode
+from ....graphql.core.utils.error_codes import ExtensionsErrorCode
from ... import ConfigurationTypeField
from ...base_plugin import BasePlugin
from . import (
@@ -439,7 +439,7 @@ class AvataxPlugin(BasePlugin):
)
raise ValidationError(
error_msg + ", ".join(missing_fields),
- code=CommonErrorCode.PLUGIN_MISCONFIGURED,
+ code=ExtensionsErrorCode.PLUGIN_MISCONFIGURED,
)
@classmethod
diff --git a/saleor/extensions/plugins/vatlayer/plugin.py b/saleor/extensions/plugins/vatlayer/plugin.py
index b830e7bc36..427d635d57 100644
--- a/saleor/extensions/plugins/vatlayer/plugin.py
+++ b/saleor/extensions/plugins/vatlayer/plugin.py
@@ -8,7 +8,7 @@ from django_prices_vatlayer.utils import get_tax_rate_types
from prices import Money, MoneyRange, TaxedMoney, TaxedMoneyRange
from ....core.taxes import TaxType
-from ....graphql.core.utils.error_codes import CommonErrorCode
+from ....graphql.core.utils.error_codes import ExtensionsErrorCode
from ...base_plugin import BasePlugin
from . import (
DEFAULT_TAX_RATE_NAME,
@@ -329,7 +329,7 @@ class VatlayerPlugin(BasePlugin):
if not settings.VATLAYER_ACCESS_KEY and plugin_configuration.active:
raise ValidationError(
"Cannot be enabled without provided 'settings.VATLAYER_ACCESS_KEY'",
- code=CommonErrorCode.PLUGIN_MISCONFIGURED,
+ code=ExtensionsErrorCode.PLUGIN_MISCONFIGURED,
)
@classmethod
diff --git a/saleor/graphql/core/enums.py b/saleor/graphql/core/enums.py
index 132192ce30..b09a072b09 100644
--- a/saleor/graphql/core/enums.py
+++ b/saleor/graphql/core/enums.py
@@ -65,3 +65,8 @@ CheckoutErrorCode = graphene.Enum.from_enum(error_codes.CheckoutErrorCode)
MenuErrorCode = graphene.Enum.from_enum(error_codes.MenuErrorCode)
OrderErrorCode = graphene.Enum.from_enum(error_codes.OrderErrorCode)
ProductErrorCode = graphene.Enum.from_enum(error_codes.ProductErrorCode)
+ShopErrorCode = graphene.Enum.from_enum(error_codes.ShopErrorCode)
+ShippingErrorCode = graphene.Enum.from_enum(error_codes.ShippingErrorCode)
+PaymentErrorCode = graphene.Enum.from_enum(error_codes.PaymentErrorCode)
+GiftcardErrorCode = graphene.Enum.from_enum(error_codes.GiftcardErrorCode)
+ExtensionsErrorCode = graphene.Enum.from_enum(error_codes.ExtensionsErrorCode)
diff --git a/saleor/graphql/core/types/common.py b/saleor/graphql/core/types/common.py
index 201f4048bf..0cde4ebfad 100644
--- a/saleor/graphql/core/types/common.py
+++ b/saleor/graphql/core/types/common.py
@@ -5,10 +5,15 @@ from ...translations.enums import LanguageCodeEnum
from ..enums import (
AccountErrorCode,
CheckoutErrorCode,
+ ExtensionsErrorCode,
+ GiftcardErrorCode,
MenuErrorCode,
OrderErrorCode,
+ PaymentErrorCode,
PermissionEnum,
ProductErrorCode,
+ ShippingErrorCode,
+ ShopErrorCode,
)
from .money import VAT
@@ -52,6 +57,26 @@ class ProductError(Error):
code = ProductErrorCode(description="The error code.")
+class ShopError(Error):
+ code = ShopErrorCode(description="The error code.")
+
+
+class ShippingError(Error):
+ code = ShippingErrorCode(description="The error code.")
+
+
+class PaymentError(Error):
+ code = PaymentErrorCode(description="The error code.")
+
+
+class GiftcardError(Error):
+ code = GiftcardErrorCode(description="The error code.")
+
+
+class ExtensionsError(Error):
+ code = ExtensionsErrorCode(description="The error code.")
+
+
class LanguageDisplay(graphene.ObjectType):
code = LanguageCodeEnum(description="Language code.", required=True)
language = graphene.String(description="Language.", required=True)
diff --git a/saleor/graphql/core/utils/error_codes.py b/saleor/graphql/core/utils/error_codes.py
index 8d401dd8aa..75dde4c4a3 100644
--- a/saleor/graphql/core/utils/error_codes.py
+++ b/saleor/graphql/core/utils/error_codes.py
@@ -162,6 +162,7 @@ class ShippingErrorCode(Enum):
class PaymentErrorCode(Enum):
+ BILLING_ADDRESS_NOT_SET = "billing_address_not_set"
GRAPHQL_ERROR = "graphql_error"
INVALID = "invalid"
NULL = "null"
@@ -176,6 +177,7 @@ class GiftcardErrorCode(Enum):
GRAPHQL_ERROR = "graphql_error"
INVALID = "invalid"
NULL = "null"
+ OBJECT_ALREADY_EXISTS = "object_already_exists"
OBJECT_DOES_NOT_EXIST = "object_does_not_exist"
REQUIRED = "required"
UNIQUE = "unique"
diff --git a/saleor/graphql/extensions/mutations.py b/saleor/graphql/extensions/mutations.py
index 1b145d8f7f..f401142a7c 100644
--- a/saleor/graphql/extensions/mutations.py
+++ b/saleor/graphql/extensions/mutations.py
@@ -2,9 +2,27 @@ import graphene
from ...extensions.manager import get_extensions_manager
from ..core.mutations import BaseMutation
+from ..core.types.common import ExtensionsError
from .types import Plugin
+class ExtensionsErrorMixin:
+ extensions_errors = graphene.List(
+ 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
+ )
+
+
class ConfigurationItemInput(graphene.InputObjectType):
name = graphene.String(required=True, description="Name of the field to update")
value = graphene.String(
@@ -23,7 +41,7 @@ class PluginUpdateInput(graphene.InputObjectType):
)
-class PluginUpdate(BaseMutation):
+class PluginUpdate(ExtensionsErrorMixin, BaseMutation):
plugin = graphene.Field(Plugin)
class Arguments:
diff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.py
index bff22cb4f4..92afbad679 100644
--- a/saleor/graphql/giftcard/mutations.py
+++ b/saleor/graphql/giftcard/mutations.py
@@ -11,10 +11,33 @@ from ...giftcard import models
from ...giftcard.utils import activate_gift_card, deactivate_gift_card
from ..core.mutations import BaseMutation, ModelMutation
from ..core.scalars import Decimal
-from ..core.utils.error_codes import CommonErrorCode
+from ..core.types.common import GiftcardError
+from ..core.utils.error_codes import GiftcardErrorCode
from .types import GiftCard
+class GiftcardErrorMixin:
+ gift_card_errors = graphene.List(
+ 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
+ )
+
+
+class BaseGiftcardMutation(GiftcardErrorMixin, BaseMutation):
+ class Meta:
+ abstract = True
+
+
class GiftCardUpdateInput(graphene.InputObjectType):
start_date = graphene.types.datetime.Date(
description="Start date of the gift card in ISO 8601 format."
@@ -32,7 +55,7 @@ class GiftCardCreateInput(GiftCardUpdateInput):
code = graphene.String(required=False, decription="Code to use the gift card.")
-class GiftCardCreate(ModelMutation):
+class GiftCardCreate(GiftcardErrorMixin, ModelMutation):
class Arguments:
input = GiftCardCreateInput(
required=True, description="Fields required to create a gift card."
@@ -64,7 +87,7 @@ class GiftCardCreate(ModelMutation):
{
"email": ValidationError(
"Customer with this email doesn't exist.",
- code=CommonErrorCode.OBJECT_DOES_NOT_EXIST,
+ code=GiftcardErrorCode.OBJECT_DOES_NOT_EXIST,
)
}
)
@@ -84,7 +107,7 @@ class GiftCardUpdate(GiftCardCreate):
permissions = ("giftcard.manage_gift_card",)
-class GiftCardDeactivate(BaseMutation):
+class GiftCardDeactivate(BaseGiftcardMutation):
gift_card = graphene.Field(GiftCard, description="A gift card to deactivate.")
class Arguments:
@@ -104,7 +127,7 @@ class GiftCardDeactivate(BaseMutation):
return GiftCardDeactivate(gift_card=gift_card)
-class GiftCardActivate(BaseMutation):
+class GiftCardActivate(BaseGiftcardMutation):
gift_card = graphene.Field(GiftCard, description="A gift card to activate.")
class Arguments:
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
index 3c8c66c910..d86bd6a2f3 100644
--- a/saleor/graphql/payment/mutations.py
+++ b/saleor/graphql/payment/mutations.py
@@ -17,12 +17,35 @@ from ..account.types import AddressInput
from ..checkout.types import Checkout
from ..core.mutations import BaseMutation
from ..core.scalars import Decimal
+from ..core.types import common as common_types
from ..core.utils import from_global_id_strict_type
-from ..core.utils.error_codes import CheckoutErrorCode, CommonErrorCode
+from ..core.utils.error_codes import PaymentErrorCode
from .enums import PaymentGatewayEnum
from .types import Payment
+class PaymentErrorMixin:
+ payment_errors = graphene.List(
+ 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
+ )
+
+
+class BasePaymentMutation(PaymentErrorMixin, BaseMutation):
+ class Meta:
+ abstract = True
+
+
class PaymentInput(graphene.InputObjectType):
gateway = graphene.Field(
PaymentGatewayEnum,
@@ -50,7 +73,7 @@ class PaymentInput(graphene.InputObjectType):
)
-class CheckoutPaymentCreate(BaseMutation, I18nMixin):
+class CheckoutPaymentCreate(BasePaymentMutation, I18nMixin):
checkout = graphene.Field(Checkout, description="Related checkout object.")
payment = graphene.Field(Payment, description="A newly created payment.")
@@ -81,7 +104,7 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
{
"billing_address": ValidationError(
"No billing address associated with this checkout.",
- code=CheckoutErrorCode.BILLING_ADDRESS_NOT_SET,
+ code=PaymentErrorCode.BILLING_ADDRESS_NOT_SET,
)
}
)
@@ -100,7 +123,7 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
"amount": ValidationError(
"Partial payments are not allowed, amount should be "
"equal checkout's total.",
- code=CommonErrorCode.PARTIAL_PAYMENT_NOT_ALLOWED,
+ code=PaymentErrorCode.PARTIAL_PAYMENT_NOT_ALLOWED,
)
}
)
@@ -121,7 +144,7 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
return CheckoutPaymentCreate(payment=payment)
-class PaymentCapture(BaseMutation):
+class PaymentCapture(BasePaymentMutation):
payment = graphene.Field(Payment, description="Updated payment")
class Arguments:
@@ -140,7 +163,7 @@ class PaymentCapture(BaseMutation):
try:
gateway_capture(payment, amount)
except PaymentError as e:
- raise ValidationError(str(e), code=CommonErrorCode.PAYMENT_ERROR)
+ raise ValidationError(str(e), code=PaymentErrorCode.PAYMENT_ERROR)
return PaymentCapture(payment=payment)
@@ -157,11 +180,11 @@ class PaymentRefund(PaymentCapture):
try:
gateway_refund(payment, amount=amount)
except PaymentError as e:
- raise ValidationError(str(e), code=CommonErrorCode.PAYMENT_ERROR)
+ raise ValidationError(str(e), code=PaymentErrorCode.PAYMENT_ERROR)
return PaymentRefund(payment=payment)
-class PaymentVoid(BaseMutation):
+class PaymentVoid(BasePaymentMutation):
payment = graphene.Field(Payment, description="Updated payment")
class Arguments:
@@ -179,11 +202,11 @@ class PaymentVoid(BaseMutation):
try:
gateway_void(payment)
except PaymentError as e:
- raise ValidationError(str(e), code=CommonErrorCode.PAYMENT_ERROR)
+ raise ValidationError(str(e), code=PaymentErrorCode.PAYMENT_ERROR)
return PaymentVoid(payment=payment)
-class PaymentSecureConfirm(BaseMutation):
+class PaymentSecureConfirm(BasePaymentMutation):
payment = graphene.Field(Payment, description="Updated payment")
class Arguments:
@@ -200,5 +223,5 @@ class PaymentSecureConfirm(BaseMutation):
try:
gateway_confirm(payment)
except PaymentError as e:
- raise ValidationError(str(e), code=CommonErrorCode.PAYMENT_ERROR)
+ raise ValidationError(str(e), code=PaymentErrorCode.PAYMENT_ERROR)
return PaymentSecureConfirm(payment=payment)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index b455685ba5..f7f4a00956 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -445,12 +445,14 @@ type AuthorizationKey {
type AuthorizationKeyAdd {
errors: [Error!]
+ shopErrors: [ShopError!]
authorizationKey: AuthorizationKey
shop: Shop
}
type AuthorizationKeyDelete {
errors: [Error!]
+ shopErrors: [ShopError!]
authorizationKey: AuthorizationKey
shop: Shop
}
@@ -756,6 +758,7 @@ type CheckoutLinesUpdate {
type CheckoutPaymentCreate {
errors: [Error!]
+ paymentErrors: [PaymentError!]
checkout: Checkout
payment: Payment
}
@@ -1531,6 +1534,21 @@ type Error {
message: String
}
+type ExtensionsError {
+ field: String
+ message: String
+ code: ExtensionsErrorCode
+}
+
+enum ExtensionsErrorCode {
+ GRAPHQL_ERROR
+ INVALID
+ NULL
+ PLUGIN_MISCONFIGURED
+ REQUIRED
+ UNIQUE
+}
+
type Fulfillment implements Node {
id: ID!
fulfillmentOrder: Int!
@@ -1622,6 +1640,7 @@ type GiftCard implements Node {
type GiftCardActivate {
errors: [Error!]
+ giftCardErrors: [GiftcardError!]
giftCard: GiftCard
}
@@ -1638,6 +1657,7 @@ type GiftCardCountableEdge {
type GiftCardCreate {
errors: [Error!]
+ giftCardErrors: [GiftcardError!]
giftCard: GiftCard
}
@@ -1651,11 +1671,13 @@ input GiftCardCreateInput {
type GiftCardDeactivate {
errors: [Error!]
+ giftCardErrors: [GiftcardError!]
giftCard: GiftCard
}
type GiftCardUpdate {
errors: [Error!]
+ giftCardErrors: [GiftcardError!]
giftCard: GiftCard
}
@@ -1666,8 +1688,25 @@ input GiftCardUpdateInput {
userEmail: String
}
+type GiftcardError {
+ field: String
+ message: String
+ code: GiftcardErrorCode
+}
+
+enum GiftcardErrorCode {
+ GRAPHQL_ERROR
+ INVALID
+ NULL
+ OBJECT_ALREADY_EXISTS
+ OBJECT_DOES_NOT_EXIST
+ REQUIRED
+ UNIQUE
+}
+
type HomepageCollectionUpdate {
errors: [Error!]
+ shopErrors: [ShopError!]
shop: Shop
}
@@ -2617,6 +2656,7 @@ type Payment implements Node {
type PaymentCapture {
errors: [Error!]
+ paymentErrors: [PaymentError!]
payment: Payment
}
@@ -2639,6 +2679,24 @@ type PaymentCountableEdge {
cursor: String!
}
+type PaymentError {
+ field: String
+ message: String
+ code: PaymentErrorCode
+}
+
+enum PaymentErrorCode {
+ BILLING_ADDRESS_NOT_SET
+ GRAPHQL_ERROR
+ INVALID
+ NULL
+ OBJECT_DOES_NOT_EXIST
+ PARTIAL_PAYMENT_NOT_ALLOWED
+ PAYMENT_ERROR
+ REQUIRED
+ UNIQUE
+}
+
input PaymentInput {
gateway: GatewaysEnum!
token: String!
@@ -2648,11 +2706,13 @@ input PaymentInput {
type PaymentRefund {
errors: [Error!]
+ paymentErrors: [PaymentError!]
payment: Payment
}
type PaymentSecureConfirm {
errors: [Error!]
+ paymentErrors: [PaymentError!]
payment: Payment
}
@@ -2663,6 +2723,7 @@ type PaymentSource {
type PaymentVoid {
errors: [Error!]
+ paymentErrors: [PaymentError!]
payment: Payment
}
@@ -2714,6 +2775,7 @@ input PluginFilterInput {
type PluginUpdate {
errors: [Error!]
+ extensionsErrors: [ExtensionsError!]
plugin: Plugin
}
@@ -3472,6 +3534,23 @@ type SetPassword {
accountErrors: [AccountError!]
}
+type ShippingError {
+ field: String
+ message: String
+ code: ShippingErrorCode
+}
+
+enum ShippingErrorCode {
+ GRAPHQL_ERROR
+ INVALID
+ MAX_LESS_THAN_MIN
+ NULL
+ OBJECT_ALREADY_EXISTS
+ OBJECT_DOES_NOT_EXIST
+ REQUIRED
+ UNIQUE
+}
+
type ShippingMethod implements Node {
id: ID!
name: String!
@@ -3502,12 +3581,14 @@ type ShippingPriceBulkDelete {
type ShippingPriceCreate {
errors: [Error!]
+ shippingErrors: [ShippingError!]
shippingZone: ShippingZone
shippingMethod: ShippingMethod
}
type ShippingPriceDelete {
errors: [Error!]
+ shippingErrors: [ShippingError!]
shippingMethod: ShippingMethod
shippingZone: ShippingZone
}
@@ -3530,6 +3611,7 @@ type ShippingPriceTranslate {
type ShippingPriceUpdate {
errors: [Error!]
+ shippingErrors: [ShippingError!]
shippingZone: ShippingZone
shippingMethod: ShippingMethod
}
@@ -3561,11 +3643,13 @@ type ShippingZoneCountableEdge {
type ShippingZoneCreate {
errors: [Error!]
+ shippingErrors: [ShippingError!]
shippingZone: ShippingZone
}
type ShippingZoneDelete {
errors: [Error!]
+ shippingErrors: [ShippingError!]
shippingZone: ShippingZone
}
@@ -3577,6 +3661,7 @@ input ShippingZoneInput {
type ShippingZoneUpdate {
errors: [Error!]
+ shippingErrors: [ShippingError!]
shippingZone: ShippingZone
}
@@ -3610,16 +3695,36 @@ type Shop {
type ShopAddressUpdate {
errors: [Error!]
+ shopErrors: [ShopError!]
shop: Shop
}
type ShopDomainUpdate {
errors: [Error!]
+ shopErrors: [ShopError!]
shop: Shop
}
+type ShopError {
+ field: String
+ message: String
+ code: ShopErrorCode
+}
+
+enum ShopErrorCode {
+ CANNOT_FETCH_TAX_RATES
+ GRAPHQL_ERROR
+ INVALID
+ NULL
+ OBJECT_ALREADY_EXISTS
+ OBJECT_DOES_NOT_EXIST
+ REQUIRED
+ UNIQUE
+}
+
type ShopFetchTaxRates {
errors: [Error!]
+ shopErrors: [ShopError!]
shop: Shop
}
@@ -3648,6 +3753,7 @@ input ShopSettingsTranslationInput {
type ShopSettingsUpdate {
errors: [Error!]
+ shopErrors: [ShopError!]
shop: Shop
}
diff --git a/saleor/graphql/shipping/mutations.py b/saleor/graphql/shipping/mutations.py
index e6c7fefa8e..ffbad016f7 100644
--- a/saleor/graphql/shipping/mutations.py
+++ b/saleor/graphql/shipping/mutations.py
@@ -5,11 +5,34 @@ from ...dashboard.shipping.forms import default_shipping_zone_exists
from ...shipping import models
from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ..core.scalars import Decimal, WeightScalar
-from ..core.utils.error_codes import CommonErrorCode
+from ..core.types.common import ShippingError
+from ..core.utils.error_codes import ShippingErrorCode
from .enums import ShippingMethodTypeEnum
from .types import ShippingMethod, ShippingZone
+class ShippingErrorMixin:
+ shipping_errors = graphene.List(
+ 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
+ )
+
+
+class BaseShippingMutation(ShippingErrorMixin, BaseMutation):
+ class Meta:
+ abstract = True
+
+
class ShippingPriceInput(graphene.InputObjectType):
name = graphene.String(description="Name of the shipping method.")
price = Decimal(description="Shipping price of the shipping method.")
@@ -56,7 +79,7 @@ class ShippingZoneMixin:
{
"default": ValidationError(
"Default shipping zone already exists.",
- code=CommonErrorCode.OBJECT_ALREADY_EXISTS,
+ code=ShippingErrorCode.OBJECT_ALREADY_EXISTS,
)
}
)
@@ -67,7 +90,7 @@ class ShippingZoneMixin:
return cleaned_input
-class ShippingZoneCreate(ShippingZoneMixin, ModelMutation):
+class ShippingZoneCreate(ShippingZoneMixin, ShippingErrorMixin, ModelMutation):
shipping_zone = graphene.Field(ShippingZone, description="Created shipping zone.")
class Arguments:
@@ -81,7 +104,7 @@ class ShippingZoneCreate(ShippingZoneMixin, ModelMutation):
permissions = ("shipping.manage_shipping",)
-class ShippingZoneUpdate(ShippingZoneMixin, ModelMutation):
+class ShippingZoneUpdate(ShippingZoneMixin, ShippingErrorMixin, ModelMutation):
shipping_zone = graphene.Field(ShippingZone, description="Updated shipping zone.")
class Arguments:
@@ -96,7 +119,7 @@ class ShippingZoneUpdate(ShippingZoneMixin, ModelMutation):
permissions = ("shipping.manage_shipping",)
-class ShippingZoneDelete(ModelDeleteMutation):
+class ShippingZoneDelete(ShippingErrorMixin, ModelDeleteMutation):
class Arguments:
id = graphene.ID(required=True, description="ID of a shipping zone to delete.")
@@ -140,7 +163,7 @@ class ShippingPriceMixin:
"Maximum order price should be larger than "
"the minimum order price."
),
- code=CommonErrorCode.MAX_LESS_THAN_MIN,
+ code=ShippingErrorCode.MAX_LESS_THAN_MIN,
)
}
)
@@ -159,14 +182,14 @@ class ShippingPriceMixin:
"Maximum order weight should be larger than the "
"minimum order weight."
),
- code=CommonErrorCode.MAX_LESS_THAN_MIN,
+ code=ShippingErrorCode.MAX_LESS_THAN_MIN,
)
}
)
return cleaned_input
-class ShippingPriceCreate(ShippingPriceMixin, ModelMutation):
+class ShippingPriceCreate(ShippingPriceMixin, ShippingErrorMixin, ModelMutation):
shipping_zone = graphene.Field(
ShippingZone,
description="A shipping zone to which the shipping method belongs.",
@@ -189,7 +212,7 @@ class ShippingPriceCreate(ShippingPriceMixin, ModelMutation):
return response
-class ShippingPriceUpdate(ShippingPriceMixin, ModelMutation):
+class ShippingPriceUpdate(ShippingPriceMixin, ShippingErrorMixin, ModelMutation):
shipping_zone = graphene.Field(
ShippingZone,
description="A shipping zone to which the shipping method belongs.",
@@ -213,7 +236,7 @@ class ShippingPriceUpdate(ShippingPriceMixin, ModelMutation):
return response
-class ShippingPriceDelete(BaseMutation):
+class ShippingPriceDelete(BaseShippingMutation):
shipping_method = graphene.Field(
ShippingMethod, description="A shipping method to delete."
)
diff --git a/saleor/graphql/shop/mutations.py b/saleor/graphql/shop/mutations.py
index 1291d4ca03..5a8824ae0b 100644
--- a/saleor/graphql/shop/mutations.py
+++ b/saleor/graphql/shop/mutations.py
@@ -9,11 +9,31 @@ from ..account.i18n import I18nMixin
from ..account.types import AddressInput
from ..core.enums import WeightUnitsEnum
from ..core.mutations import BaseMutation
-from ..core.utils.error_codes import CommonErrorCode
+from ..core.types.common import ShopError
+from ..core.utils.error_codes import ShopErrorCode
from ..product.types import Collection
from .types import AuthorizationKey, AuthorizationKeyType, Shop
+class ShopErrorMixin:
+ shop_errors = graphene.List(
+ 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)
+
+
+class BaseShopMutation(ShopErrorMixin, BaseMutation):
+ class Meta:
+ abstract = True
+
+
class ShopSettingsInput(graphene.InputObjectType):
header_text = graphene.String(description="Header text")
description = graphene.String(description="SEO description")
@@ -42,7 +62,7 @@ class SiteDomainInput(graphene.InputObjectType):
name = graphene.String(description="Shop site name")
-class ShopSettingsUpdate(BaseMutation):
+class ShopSettingsUpdate(BaseShopMutation):
shop = graphene.Field(Shop, description="Updated Shop")
class Arguments:
@@ -67,7 +87,7 @@ class ShopSettingsUpdate(BaseMutation):
return ShopSettingsUpdate(shop=Shop())
-class ShopAddressUpdate(BaseMutation, I18nMixin):
+class ShopAddressUpdate(BaseShopMutation, I18nMixin):
shop = graphene.Field(Shop, description="Updated Shop")
class Arguments:
@@ -93,7 +113,7 @@ class ShopAddressUpdate(BaseMutation, I18nMixin):
return ShopAddressUpdate(shop=Shop())
-class ShopDomainUpdate(BaseMutation):
+class ShopDomainUpdate(BaseShopMutation):
shop = graphene.Field(Shop, description="Updated Shop")
class Arguments:
@@ -118,7 +138,7 @@ class ShopDomainUpdate(BaseMutation):
return ShopDomainUpdate(shop=Shop())
-class ShopFetchTaxRates(BaseMutation):
+class ShopFetchTaxRates(BaseShopMutation):
shop = graphene.Field(Shop, description="Updated Shop")
class Meta:
@@ -131,13 +151,13 @@ class ShopFetchTaxRates(BaseMutation):
raise ValidationError(
"Could not fetch tax rates. Make sure you have supplied a "
"valid API Access Key.",
- code=CommonErrorCode.CANNOT_FETCH_TAX_RATES,
+ code=ShopErrorCode.CANNOT_FETCH_TAX_RATES,
)
call_command("get_vat_rates")
return ShopFetchTaxRates(shop=Shop())
-class HomepageCollectionUpdate(BaseMutation):
+class HomepageCollectionUpdate(BaseShopMutation):
shop = graphene.Field(Shop, description="Updated Shop")
class Arguments:
@@ -166,7 +186,7 @@ class AuthorizationKeyInput(graphene.InputObjectType):
password = graphene.String(required=True, description="Client secret.")
-class AuthorizationKeyAdd(BaseMutation):
+class AuthorizationKeyAdd(BaseShopMutation):
authorization_key = graphene.Field(
AuthorizationKey, description="Newly added authorization key."
)
@@ -191,7 +211,7 @@ class AuthorizationKeyAdd(BaseMutation):
{
"key_type": ValidationError(
"Authorization key already exists.",
- code=CommonErrorCode.OBJECT_ALREADY_EXISTS,
+ code=ShopErrorCode.OBJECT_ALREADY_EXISTS,
)
}
)
@@ -205,7 +225,7 @@ class AuthorizationKeyAdd(BaseMutation):
return AuthorizationKeyAdd(authorization_key=instance, shop=Shop())
-class AuthorizationKeyDelete(BaseMutation):
+class AuthorizationKeyDelete(BaseShopMutation):
authorization_key = graphene.Field(
AuthorizationKey, description="Authorization key that was deleted."
)
@@ -232,7 +252,7 @@ class AuthorizationKeyDelete(BaseMutation):
{
"key_type": ValidationError(
"Couldn't resolve authorization key",
- code=CommonErrorCode.OBJECT_DOES_NOT_EXIST,
+ code=ShopErrorCode.OBJECT_DOES_NOT_EXIST,
)
}
)
diff --git a/tests/api/test_discount.py b/tests/api/test_discount.py
index b702f753ee..2149a7dad1 100644
--- a/tests/api/test_discount.py
+++ b/tests/api/test_discount.py
@@ -8,7 +8,6 @@ from django_countries import countries
from saleor.discount import DiscountValueType, VoucherType
from saleor.discount.models import Sale, Voucher
-from saleor.graphql.core.utils.error_codes import CommonErrorCode
from saleor.graphql.discount.enums import DiscountValueTypeEnum, VoucherTypeEnum
from tests.api.utils import get_graphql_content
@@ -157,7 +156,6 @@ mutation voucherCreate(
errors {
field
message
- code
}
voucher {
type
@@ -265,7 +263,6 @@ def test_create_voucher_with_existing_gift_card_code(
errors = content["data"]["voucherCreate"]["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "promoCode"
- assert errors[0]["code"] == CommonErrorCode.OBJECT_ALREADY_EXISTS.name
def test_create_voucher_with_existing_voucher_code(
@@ -292,7 +289,6 @@ def test_create_voucher_with_existing_voucher_code(
errors = content["data"]["voucherCreate"]["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "promoCode"
- assert errors[0]["code"] == CommonErrorCode.OBJECT_ALREADY_EXISTS.name
def test_update_voucher(staff_api_client, voucher, permission_manage_discounts):
diff --git a/tests/api/test_gift_card.py b/tests/api/test_gift_card.py
index b876d10931..b92680e937 100644
--- a/tests/api/test_gift_card.py
+++ b/tests/api/test_gift_card.py
@@ -2,7 +2,6 @@ from datetime import date
import graphene
-from saleor.graphql.core.utils.error_codes import CommonErrorCode
from tests.api.utils import get_graphql_content
from .utils import assert_no_permission
@@ -172,6 +171,10 @@ mutation giftCardCreate(
errors {
field
message
+ }
+ giftCardErrors {
+ field
+ message
code
}
giftCard {
@@ -284,7 +287,9 @@ def test_create_gift_card_with_existing_voucher_code(
errors = content["data"]["giftCardCreate"]["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "promoCode"
- assert errors[0]["code"] == CommonErrorCode.OBJECT_ALREADY_EXISTS.name
+
+ gift_card_errors = content["data"]["giftCardCreate"]["giftCardErrors"]
+ assert gift_card_errors[0]["code"] == "OBJECT_ALREADY_EXISTS"
def test_create_gift_card_with_existing_gift_card_code(
@@ -300,7 +305,9 @@ def test_create_gift_card_with_existing_gift_card_code(
errors = content["data"]["giftCardCreate"]["errors"]
assert len(errors) == 1
assert errors[0]["field"] == "promoCode"
- assert errors[0]["code"] == CommonErrorCode.OBJECT_ALREADY_EXISTS.name
+
+ gift_card_errors = content["data"]["giftCardCreate"]["giftCardErrors"]
+ assert gift_card_errors[0]["code"] == "OBJECT_ALREADY_EXISTS"
def test_create_gift_card_without_user(staff_api_client, permission_manage_gift_card):
@@ -351,7 +358,9 @@ def test_create_gift_card_with_incorrect_user_email(
assert len(errors) == 1
assert errors[0]["field"] == "email"
assert errors[0]["message"] == "Customer with this email doesn't exist."
- assert errors[0]["code"] == CommonErrorCode.OBJECT_DOES_NOT_EXIST.name
+
+ gift_card_errors = content["data"]["giftCardCreate"]["giftCardErrors"]
+ assert gift_card_errors[0]["code"] == "OBJECT_DOES_NOT_EXIST"
def test_create_gift_card_without_premissions(staff_api_client):