Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Deal with errors in precise manner
Tomasz Szymanskifb68358ea84e
diff --git a/saleor/graphql/core/enums.py b/saleor/graphql/core/enums.pyindex ebc72fe22c..a2ee8be9af 100644--- a/saleor/graphql/core/enums.py+++ b/saleor/graphql/core/enums.py@@ -94,6 +94,7 @@ GiftCardErrorCode = graphene.Enum.from_enum(giftcard_error_codes.GiftCardErrorCoMenuErrorCode = graphene.Enum.from_enum(menu_error_codes.MenuErrorCode)MetadataErrorCode = graphene.Enum.from_enum(core_error_codes.MetadataErrorCode)OrderErrorCode = graphene.Enum.from_enum(order_error_codes.OrderErrorCode)+InvoiceErrorCode = graphene.Enum.from_enum(order_error_codes.InvoiceErrorCode)PageErrorCode = graphene.Enum.from_enum(page_error_codes.PageErrorCode)PaymentErrorCode = graphene.Enum.from_enum(payment_error_codes.PaymentErrorCode)ProductErrorCode = graphene.Enum.from_enum(product_error_codes.ProductErrorCode)diff --git a/saleor/graphql/core/types/common.py b/saleor/graphql/core/types/common.pyindex 5d3777157f..555b3d80da 100644--- a/saleor/graphql/core/types/common.py+++ b/saleor/graphql/core/types/common.py@@ -8,6 +8,7 @@ from ..enums import (DiscountErrorCode,ExtensionsErrorCode,GiftCardErrorCode,+ InvoiceErrorCode,MenuErrorCode,MetadataErrorCode,OrderErrorCode,@@ -69,6 +70,10 @@ class OrderError(Error):code = OrderErrorCode(description="The error code.", required=True)+class InvoiceError(Error):+ code = InvoiceErrorCode(description="The error code.", required=True)++class ProductError(Error):code = ProductErrorCode(description="The error code.", required=True)diff --git a/saleor/graphql/core/utils/error_codes.py b/saleor/graphql/core/utils/error_codes.pyindex b8474d8a37..759bbfb320 100644--- a/saleor/graphql/core/utils/error_codes.py+++ b/saleor/graphql/core/utils/error_codes.py@@ -7,7 +7,7 @@ from ....discount.error_codes import DiscountErrorCodefrom ....extensions.error_codes import ExtensionsErrorCodefrom ....giftcard.error_codes import GiftCardErrorCodefrom ....menu.error_codes import MenuErrorCode-from ....order.error_codes import OrderErrorCode+from ....order.error_codes import InvoiceErrorCode, OrderErrorCodefrom ....payment.error_codes import PaymentErrorCodefrom ....product.error_codes import ProductErrorCodefrom ....shipping.error_codes import ShippingErrorCode@@ -46,6 +46,7 @@ SALEOR_ERROR_CODE_ENUMS = [DiscountErrorCode,ExtensionsErrorCode,GiftCardErrorCode,+ InvoiceErrorCode,MenuErrorCode,OrderErrorCode,PaymentErrorCode,diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.pyindex d7fa04a3c3..673fa344af 100644--- a/saleor/graphql/order/mutations/orders.py+++ b/saleor/graphql/order/mutations/orders.py@@ -20,7 +20,7 @@ from ....payment import CustomPaymentChoices, PaymentError, gatewayfrom ...account.types import AddressInputfrom ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutationfrom ...core.scalars import Decimal-from ...core.types.common import OrderError+from ...core.types.common import InvoiceError, OrderErrorfrom ...meta.deprecated.mutations import ClearMetaBaseMutation, UpdateMetaBaseMutationfrom ...meta.deprecated.types import MetaInput, MetaPathfrom ...order.enums import InvoiceStatus@@ -521,6 +521,8 @@ class RequestInvoice(BaseMutation):class Meta:description = "Request an invoice for the order."permissions = (OrderPermissions.MANAGE_ORDERS,)+ error_type_class = InvoiceError+ error_type_field = "invoice_errors"class Arguments:order_id = graphene.ID(@@ -553,6 +555,8 @@ class DeleteInvoice(ModelDeleteMutation):description = "Deletes an invoice."model = models.Invoicepermissions = (OrderPermissions.MANAGE_ORDERS,)+ error_type_class = InvoiceError+ error_type_field = "invoice_errors"@classmethoddef perform_mutation(cls, _root, info, **data):@@ -572,6 +576,8 @@ class UpdateInvoice(ModelMutation):description = "Updates an invoice."model = models.Invoicepermissions = (OrderPermissions.MANAGE_ORDERS,)+ error_type_class = InvoiceError+ error_type_field = "invoice_errors"@classmethoddef clean_input(cls, info, instance, data):diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex c3c02a1b4b..45bf46a3d3 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -1419,6 +1419,7 @@ scalar Decimaltype DeleteInvoice {errors: [Error!]!+ invoiceErrors: [InvoiceError!]!invoice: Invoice}@@ -1861,6 +1862,17 @@ type Invoice implements Node {url: String}+type InvoiceError {+ field: String+ message: String+ code: InvoiceErrorCode!+}++enum InvoiceErrorCode {+ URL_OR_NUMBER_NOT_SET+ NOT_FOUND+}+scalar JSONStringenum LanguageCodeEnum {@@ -3825,6 +3837,7 @@ type RequestEmailChange {type RequestInvoice {errors: [Error!]!+ invoiceErrors: [InvoiceError!]!}type RequestPasswordReset {@@ -4564,6 +4577,7 @@ scalar UUIDtype UpdateInvoice {errors: [Error!]!+ invoiceErrors: [InvoiceError!]!invoice: Invoice}diff --git a/saleor/order/error_codes.py b/saleor/order/error_codes.pyindex b37c74549c..00f4d4c72b 100644--- a/saleor/order/error_codes.py+++ b/saleor/order/error_codes.py@@ -26,3 +26,4 @@ class OrderErrorCode(Enum):class InvoiceErrorCode(Enum):URL_OR_NUMBER_NOT_SET = "url_or_number_not_set"+ NOT_FOUND = "not_found"diff --git a/tests/api/test_order.py b/tests/api/test_order.pyindex 6ea39ea792..8c85faf54d 100644--- a/tests/api/test_order.py+++ b/tests/api/test_order.py@@ -24,7 +24,7 @@ from saleor.graphql.order.mutations.orders import (from saleor.graphql.order.utils import validate_draft_orderfrom saleor.graphql.payment.types import PaymentChargeStatusEnumfrom saleor.order import OrderStatus, events as order_events-from saleor.order.error_codes import OrderErrorCode+from saleor.order.error_codes import InvoiceErrorCode, OrderErrorCodefrom saleor.order.models import Invoice, Order, OrderEventfrom saleor.payment import ChargeStatus, CustomPaymentChoices, PaymentErrorfrom saleor.payment.models import Payment@@ -2821,9 +2821,10 @@ REQUEST_INVOICE_MUTATION = """orderId: $orderId,number: $number) {- errors {+ invoiceErrors {fieldmessage+ code}}}@@ -2835,9 +2836,10 @@ DELETE_INVOICE_MUTATION = """deleteInvoice(id: $id) {- errors {+ invoiceErrors {fieldmessage+ code}}}@@ -2855,9 +2857,10 @@ UPDATE_INVOICE_MUTATION = """numberurl}- errors {+ invoiceErrors {fieldmessage+ code}}}@@ -2892,7 +2895,9 @@ def test_request_invoice_invalid_order(user_api_client, permission_manage_ordersuser_api_client.user.user_permissions.add(permission_manage_orders)response = user_api_client.post_graphql(REQUEST_INVOICE_MUTATION, variables)content = get_graphql_content(response)- assert content["data"]["requestInvoice"]["errors"][0]["field"] == "orderId"+ errors = content["data"]["requestInvoice"]["invoiceErrors"][0]+ assert errors["code"] == InvoiceErrorCode.NOT_FOUND.name+ assert errors["field"] == "orderId"def test_request_invoice_no_permissions(staff_api_client, orders):@@ -2914,6 +2919,20 @@ def test_delete_invoice(plugin_mock, user_api_client, permission_manage_orders,assert plugin_mock.called[email protected]("saleor.extensions.base_plugin.BasePlugin.invoice_delete")+def test_delete_invoice_invalid_id(+ plugin_mock, user_api_client, permission_manage_orders, orders+):+ variables = {"id": graphene.Node.to_global_id("Invoice", 1337)}+ user_api_client.user.user_permissions.add(permission_manage_orders)+ response = user_api_client.post_graphql(DELETE_INVOICE_MUTATION, variables)+ content = get_graphql_content(response)+ errors = content["data"]["deleteInvoice"]["invoiceErrors"][0]+ assert errors["code"] == InvoiceErrorCode.NOT_FOUND.name+ assert errors["field"] == "id"+ assert not plugin_mock.called++def test_delete_invoice_no_permissions(user_api_client, orders):invoice = Invoice.objects.create(order=orders[0])variables = {"id": graphene.Node.to_global_id("Invoice", invoice.pk)}@@ -2969,7 +2988,9 @@ def test_update_invoice_missing_number(response = user_api_client.post_graphql(UPDATE_INVOICE_MUTATION, variables)content = get_graphql_content(response)invoice.refresh_from_db()- assert content["data"]["updateInvoice"]["errors"][0]["field"] == "invoice"+ errors = content["data"]["updateInvoice"]["invoiceErrors"][0]+ assert errors["code"] == InvoiceErrorCode.URL_OR_NUMBER_NOT_SET.name+ assert errors["field"] == "invoice"assert invoice.url is Noneassert invoice.status == InvoiceStatus.PENDING@@ -2990,4 +3011,6 @@ def test_update_invoice_invalid_id(user_api_client, permission_manage_orders):user_api_client.user.user_permissions.add(permission_manage_orders)response = user_api_client.post_graphql(UPDATE_INVOICE_MUTATION, variables)content = get_graphql_content(response)- assert content["data"]["updateInvoice"]["errors"][0]["field"] == "id"+ errors = content["data"]["updateInvoice"]["invoiceErrors"][0]+ assert errors["code"] == InvoiceErrorCode.NOT_FOUND.name+ assert errors["field"] == "id"