From 64e9ce380731d94d062600ea03ba51593a7e2f76 Mon Sep 17 00:00:00 2001 From: Damien Retzinger Date: Wed, 18 Sep 2024 14:13:14 -0400 Subject: [PATCH] fix(driver): allow undefined extensions key in Magento graphql responses (#3087) 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, without this patch you will see errors like `Cannot read properties of undefined (reading 'category')` as a result. --- libs/driver/magento/src/errors/transform-graphql.spec.ts | 8 ++++++-- libs/driver/magento/src/errors/transform-graphql.ts | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/libs/driver/magento/src/errors/transform-graphql.spec.ts b/libs/driver/magento/src/errors/transform-graphql.spec.ts index 2fc8e971db..f0c61609db 100644 --- a/libs/driver/magento/src/errors/transform-graphql.spec.ts +++ b/libs/driver/magento/src/errors/transform-graphql.spec.ts @@ -1,5 +1,3 @@ -import { ApolloError } from '@apollo/client/core'; - import { DaffError, DaffErrorCodeMap, @@ -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(); }); diff --git a/libs/driver/magento/src/errors/transform-graphql.ts b/libs/driver/magento/src/errors/transform-graphql.ts index 0ea549a653..c67fd06345 100644 --- a/libs/driver/magento/src/errors/transform-graphql.ts +++ b/libs/driver/magento/src/errors/transform-graphql.ts @@ -11,7 +11,7 @@ export function daffMagentoTransformGraphQlError( error: GraphQLError, map: T, ): DaffError { - const ErrorClass = map[error.extensions.category] || DaffDriverMagentoError; + const ErrorClass = map[error?.extensions?.category] || DaffDriverMagentoError; return new ErrorClass(error.message) ; };