From 0aba4a085aef6ad43bcba50ac06ba14b2b0077a5 Mon Sep 17 00:00:00 2001 From: Damien Retzinger Date: Wed, 18 Sep 2024 14:49:45 -0400 Subject: [PATCH] fix(cart): adjust Magento error handling for v2.4.6 removal of extensions (#3088) --- libs/cart/driver/magento/src/errors/map.ts | 2 + .../magento/src/errors/transform.spec.ts | 50 ++++++++----------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/libs/cart/driver/magento/src/errors/map.ts b/libs/cart/driver/magento/src/errors/map.ts index 20d09550c2..a2de8edafc 100644 --- a/libs/cart/driver/magento/src/errors/map.ts +++ b/libs/cart/driver/magento/src/errors/map.ts @@ -23,6 +23,8 @@ export const DaffCartMagentoErrorMessageRegexMap = { [DaffCartDriverErrorCodes.PRODUCT_OUT_OF_STOCK]: /(The requested qty is not available)|(This product is out of stock)|(Some of the products are out of stock)|(There are no source items with the in stock status)/, [DaffCartDriverErrorCodes.ITEM_EXCEEDS_MAX_QTY]: /The requested qty exceeds the maximum qty allowed in shopping cart/, [DaffCartDriverErrorCodes.INVALID_EMAIL]: /Invalid email format/, + [DaffCartDriverErrorCodes.CART_NOT_FOUND]: /Could not find a cart with ID/, + [DaffCartDriverErrorCodes.UNAUTHORIZED_FOR_CART]: /The current customer isn\'t authorized/, }; export const DaffCartMagentoUserErrorMap: DaffErrorCodeMap = { diff --git a/libs/cart/driver/magento/src/errors/transform.spec.ts b/libs/cart/driver/magento/src/errors/transform.spec.ts index d3c31e7939..7211ff8661 100644 --- a/libs/cart/driver/magento/src/errors/transform.spec.ts +++ b/libs/cart/driver/magento/src/errors/transform.spec.ts @@ -2,10 +2,13 @@ import { ApolloError } from '@apollo/client/core'; import { GraphQLError } from 'graphql'; import { + DaffCartNotFoundError, DaffInvalidCouponCodeError, DaffProductOutOfStockError, + DaffUnauthorizedForCartError, } from '@daffodil/cart/driver'; +import { MagentoCartGraphQlErrorCode } from './codes'; import { transformCartMagentoError } from './transform'; describe('@daffodil/cart/driver/magento | transformCartMagentoError', () => { @@ -13,41 +16,28 @@ describe('@daffodil/cart/driver/magento | transformCartMagentoError', () => { let graphQlError: GraphQLError; let transformedError: Error; - describe('when the GraphQL error is an invalid coupon code error', () => { - beforeEach(() => { - graphQlError = new GraphQLError('The coupon code isn\'t valid. Verify the code and try again.'); + it('should transform error codes correctly', () => { + const errors = [ + { message: 'The coupon code isn\'t valid. Verify the code and try again.', category: undefined, type: DaffInvalidCouponCodeError }, + { message: 'There are no source items with the in stock status', category: undefined, type: DaffProductOutOfStockError }, + { message: 'Could not find a cart with ID "asdasdasd"', category: MagentoCartGraphQlErrorCode.CART_NOT_FOUND, type: DaffCartNotFoundError }, + { message: 'Could not find a cart with ID "asdasdasd"', category: undefined, type: DaffCartNotFoundError }, + { message: 'Cart does not contain products', category: undefined, type: Error }, + { message: 'The current customer isn\'t authorized', category: undefined, type: DaffUnauthorizedForCartError }, + ]; + + errors.forEach((el) => { + graphQlError = new GraphQLError(el.message); + if(el.category) { + graphQlError.extensions.category = el.category; + } apolloError = new ApolloError({ graphQLErrors: [graphQlError], }); - - transformedError = transformCartMagentoError(apolloError); - }); - - it('should return a DaffInvalidCouponCodeError', () => { - expect(transformedError).toEqual(jasmine.any(DaffInvalidCouponCodeError)); - }); - - it('should return an error containing the GraphQL error message', () => { - expect(transformedError.message).toContain(graphQlError.message); - }); - }); - - describe('when the GraphQL error is a no source items with in stock status error', () => { - beforeEach(() => { - graphQlError = new GraphQLError('There are no source items with the in stock status'); - apolloError = new ApolloError({ - graphQLErrors: [graphQlError], - }); - transformedError = transformCartMagentoError(apolloError); - }); - - it('should return a DaffProductOutOfStockError', () => { - expect(transformedError).toEqual(jasmine.any(DaffProductOutOfStockError)); - }); - it('should return an error containing the GraphQL error message', () => { - expect(transformedError.message).toContain(graphQlError.message); + expect(transformedError).toEqual(jasmine.any(el.type)); + expect(transformedError.message).toContain(el.message); }); }); });