From 52977ba9df569fd721ee87b54657fd18fa81f0d1 Mon Sep 17 00:00:00 2001 From: Thomas Kellermeier Date: Tue, 25 Jun 2024 16:41:21 +0200 Subject: [PATCH 1/2] fix: dont crash the application when a deep-binding was not found but fall back to default value --- .../gatherAutoFetchedReferentsFromIncludes.ts | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/core/src/fetchers/gatherAutoFetchedReferentsFromIncludes.ts b/packages/core/src/fetchers/gatherAutoFetchedReferentsFromIncludes.ts index 531351098..cd4e2709b 100644 --- a/packages/core/src/fetchers/gatherAutoFetchedReferentsFromIncludes.ts +++ b/packages/core/src/fetchers/gatherAutoFetchedReferentsFromIncludes.ts @@ -37,23 +37,24 @@ export function gatherAutoFetchedReferentsFromIncludes( (entry) => entry.sys.id === reference.headEntityId, ); if (!headEntry) { - throw new Error( - `LogicError: When resolving deep-references could not find headEntry (id=${reference.entityId})`, + console.debug( + `[experiences-sdk-core::fetchers] When resolving deep-references could not find headEntry with id '${reference.entityId}'`, ); + continue; } const linkToReferent = headEntry.fields[reference.field] as UnresolvedLink<'Asset' | 'Entry'>; if (undefined === linkToReferent) { console.debug( - `[experiences-sdk-react::gatherAutoFetchedReferentsFromIncludes] Empty reference in headEntity. Probably reference is simply not set.`, + `[experiences-sdk-core::fetchers] Empty reference in headEntity. Probably reference is simply not set.`, ); continue; } if (!isLink(linkToReferent)) { console.debug( - `[experiences-sdk-react::gatherAutoFetchedReferentsFromIncludes] Non-link value in headEntity. Probably broken path '${reference.originalPath}'`, + `[experiences-sdk-core::fetchers] Non-link value in headEntity. Probably broken path '${reference.originalPath}'`, ); continue; } @@ -62,9 +63,7 @@ export function gatherAutoFetchedReferentsFromIncludes( if (!['Entry', 'Asset'].includes(linkType)) { console.debug( - `[experiences-sdk-react::gatherAutoFetchedReferentsFromIncludes] Unhandled linkType :${JSON.stringify( - linkToReferent, - )}`, + `[experiences-sdk-core::fetchers] Unhandled linkType :${JSON.stringify(linkToReferent)}`, ); continue; } @@ -73,11 +72,14 @@ export function gatherAutoFetchedReferentsFromIncludes( (entity) => entity.sys.id === linkToReferent.sys.id, ); if (!referentEntity) { - throw new Error( - `Logic Error: L2-referent ${linkType} was not found within .includes (${JSON.stringify({ - linkToReferent, - })})`, + console.debug( + `[experiences-sdk-core::fetchers] L2-referent ${linkType} was not found within .includes (${JSON.stringify( + { + linkToReferent, + }, + )})`, ); + continue; } linkType === 'Entry' From 64d310662b6bc21aecc0d7aef7c7cb3ac0b91a8e Mon Sep 17 00:00:00 2001 From: Thomas Kellermeier Date: Tue, 25 Jun 2024 16:50:53 +0200 Subject: [PATCH 2/2] test: add test case for new fall back behavior on L2 bindings --- ...erAutoFetchedReferentsFromIncludes.spec.ts | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/core/src/fetchers/gatherAutoFetchedReferentsFromIncludes.spec.ts b/packages/core/src/fetchers/gatherAutoFetchedReferentsFromIncludes.spec.ts index bf45d91f0..9b81d638e 100644 --- a/packages/core/src/fetchers/gatherAutoFetchedReferentsFromIncludes.spec.ts +++ b/packages/core/src/fetchers/gatherAutoFetchedReferentsFromIncludes.spec.ts @@ -91,7 +91,7 @@ describe('gatherAutoFetchedReferentsFromIncludes', () => { }); }); - it('throws an error if head entity not in collection', () => { + it('resolves nothing if head entity is missing', () => { const deepReferences = [ new DeepReference({ path: '/uuid1/fields/logo/~locale/fields/file/~locale', @@ -100,10 +100,29 @@ describe('gatherAutoFetchedReferentsFromIncludes', () => { ]; const newCollection = { items: [], includes: { ...collection.includes } }; - expect(() => - gatherAutoFetchedReferentsFromIncludes(deepReferences, newCollection), - ).toThrowError( - `LogicError: When resolving deep-references could not find headEntry (id=entry1)`, - ); + const result = gatherAutoFetchedReferentsFromIncludes(deepReferences, newCollection); + expect(result).toEqual({ + autoFetchedReferentAssets: [], + autoFetchedReferentEntries: [], + }); + }); + + it('resolves nothing if L2-referent is missing', () => { + const deepReferences = [ + new DeepReference({ + path: '/uuid1/fields/logo/~locale/fields/file/~locale', + dataSource, + }), + ]; + + const newCollection = { + items: [...collection.items], + includes: { Entry: [...collection.includes.Entry], Asset: [] }, + }; + const result = gatherAutoFetchedReferentsFromIncludes(deepReferences, newCollection); + expect(result).toEqual({ + autoFetchedReferentAssets: [], + autoFetchedReferentEntries: [], + }); }); });