Skip to content

Commit 0aba4a0

Browse files
authored
fix(cart): adjust Magento error handling for v2.4.6 removal of extensions (#3088)
1 parent 64e9ce3 commit 0aba4a0

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

libs/cart/driver/magento/src/errors/map.ts

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export const DaffCartMagentoErrorMessageRegexMap = {
2323
[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)/,
2424
[DaffCartDriverErrorCodes.ITEM_EXCEEDS_MAX_QTY]: /The requested qty exceeds the maximum qty allowed in shopping cart/,
2525
[DaffCartDriverErrorCodes.INVALID_EMAIL]: /Invalid email format/,
26+
[DaffCartDriverErrorCodes.CART_NOT_FOUND]: /Could not find a cart with ID/,
27+
[DaffCartDriverErrorCodes.UNAUTHORIZED_FOR_CART]: /The current customer isn\'t authorized/,
2628
};
2729

2830
export const DaffCartMagentoUserErrorMap: DaffErrorCodeMap = {

libs/cart/driver/magento/src/errors/transform.spec.ts

+20-30
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,42 @@ import { ApolloError } from '@apollo/client/core';
22
import { GraphQLError } from 'graphql';
33

44
import {
5+
DaffCartNotFoundError,
56
DaffInvalidCouponCodeError,
67
DaffProductOutOfStockError,
8+
DaffUnauthorizedForCartError,
79
} from '@daffodil/cart/driver';
810

11+
import { MagentoCartGraphQlErrorCode } from './codes';
912
import { transformCartMagentoError } from './transform';
1013

1114
describe('@daffodil/cart/driver/magento | transformCartMagentoError', () => {
1215
let apolloError: ApolloError;
1316
let graphQlError: GraphQLError;
1417
let transformedError: Error;
1518

16-
describe('when the GraphQL error is an invalid coupon code error', () => {
17-
beforeEach(() => {
18-
graphQlError = new GraphQLError('The coupon code isn\'t valid. Verify the code and try again.');
19+
it('should transform error codes correctly', () => {
20+
const errors = [
21+
{ message: 'The coupon code isn\'t valid. Verify the code and try again.', category: undefined, type: DaffInvalidCouponCodeError },
22+
{ message: 'There are no source items with the in stock status', category: undefined, type: DaffProductOutOfStockError },
23+
{ message: 'Could not find a cart with ID "asdasdasd"', category: MagentoCartGraphQlErrorCode.CART_NOT_FOUND, type: DaffCartNotFoundError },
24+
{ message: 'Could not find a cart with ID "asdasdasd"', category: undefined, type: DaffCartNotFoundError },
25+
{ message: 'Cart does not contain products', category: undefined, type: Error },
26+
{ message: 'The current customer isn\'t authorized', category: undefined, type: DaffUnauthorizedForCartError },
27+
];
28+
29+
errors.forEach((el) => {
30+
graphQlError = new GraphQLError(el.message);
31+
if(el.category) {
32+
graphQlError.extensions.category = el.category;
33+
}
1934
apolloError = new ApolloError({
2035
graphQLErrors: [graphQlError],
2136
});
22-
23-
transformedError = transformCartMagentoError(apolloError);
24-
});
25-
26-
it('should return a DaffInvalidCouponCodeError', () => {
27-
expect(transformedError).toEqual(jasmine.any(DaffInvalidCouponCodeError));
28-
});
29-
30-
it('should return an error containing the GraphQL error message', () => {
31-
expect(transformedError.message).toContain(graphQlError.message);
32-
});
33-
});
34-
35-
describe('when the GraphQL error is a no source items with in stock status error', () => {
36-
beforeEach(() => {
37-
graphQlError = new GraphQLError('There are no source items with the in stock status');
38-
apolloError = new ApolloError({
39-
graphQLErrors: [graphQlError],
40-
});
41-
4237
transformedError = transformCartMagentoError(apolloError);
43-
});
44-
45-
it('should return a DaffProductOutOfStockError', () => {
46-
expect(transformedError).toEqual(jasmine.any(DaffProductOutOfStockError));
47-
});
4838

49-
it('should return an error containing the GraphQL error message', () => {
50-
expect(transformedError.message).toContain(graphQlError.message);
39+
expect(transformedError).toEqual(jasmine.any(el.type));
40+
expect(transformedError.message).toContain(el.message);
5141
});
5242
});
5343
});

0 commit comments

Comments
 (0)