Skip to content

Commit

Permalink
fix(cart): adjust Magento error handling for v2.4.6 removal of extens…
Browse files Browse the repository at this point in the history
…ions (#3088)
  • Loading branch information
damienwebdev authored Sep 18, 2024
1 parent 64e9ce3 commit 0aba4a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
2 changes: 2 additions & 0 deletions libs/cart/driver/magento/src/errors/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
50 changes: 20 additions & 30 deletions libs/cart/driver/magento/src/errors/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,42 @@ 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', () => {
let apolloError: ApolloError;
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);
});
});
});

0 comments on commit 0aba4a0

Please sign in to comment.