Skip to content

Commit

Permalink
fix(driver): allow undefined extensions key in Magento graphql responses
Browse files Browse the repository at this point in the history
In Magento versions that support GraphQl prior to v2.4.6 there was a key errors
called "extensions" that contained an error code indicating what kind of error you received.
For whatever reason, in v2.4.6 they removed this.

Once you upgrade to v2.4.6 you will see errors like `Cannot read properties of undefined (reading 'category')` as a result.
  • Loading branch information
damienwebdev committed Sep 18, 2024
1 parent b5a39df commit e774ea4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions libs/driver/magento/src/errors/transform-graphql.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ApolloError } from '@apollo/client/core';

import {
DaffError,
DaffErrorCodeMap,
Expand Down Expand Up @@ -60,6 +58,12 @@ describe('@daffodil/driver/magento | daffMagentoTransformGraphQlError', () => {
});

it('should not crash if the extension is not defined', () => {
const { extensions, ...error } = unhandledGraphQlError;
expect(() => daffMagentoTransformGraphQlError(error, map)).not.toThrow();
expect(daffMagentoTransformGraphQlError(error, map)).toEqual(new DaffDriverMagentoError('An error we don\'t handle'));
});

it('should not crash if there are no extensions defined', () => {
const error = { ...unhandledGraphQlError, extensions: {}};
expect(() => daffMagentoTransformGraphQlError(error, map)).not.toThrow();
});
Expand Down
2 changes: 1 addition & 1 deletion libs/driver/magento/src/errors/transform-graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function daffMagentoTransformGraphQlError<T extends DaffErrorCodeMap>(
error: GraphQLError,
map: T,
): DaffError {
const ErrorClass = map[error.extensions.category] || DaffDriverMagentoError;
const ErrorClass = map[error?.extensions?.category] || DaffDriverMagentoError;

return new ErrorClass(error.message) ;
};

0 comments on commit e774ea4

Please sign in to comment.