Skip to content

fix(cart): adjust Magento error handling for v2.4.6 removal of extensions #3088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { transformCartMagentoError } from './transform';
import { MagentoCartGraphQlErrorCode } from './codes';

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);
});
});
});
Loading