From 56748b7ed456ad15bccb6ab08ceae26889cd861f Mon Sep 17 00:00:00 2001 From: henrikmv Date: Fri, 25 Oct 2024 16:24:43 +0200 Subject: [PATCH] fix: add try catch to all query calls --- .../utils/getSubValueForChangelogData.js | 80 ++++++++++++------- 1 file changed, 50 insertions(+), 30 deletions(-) diff --git a/src/core_modules/capture-core/components/WidgetsChangelog/common/utils/getSubValueForChangelogData.js b/src/core_modules/capture-core/components/WidgetsChangelog/common/utils/getSubValueForChangelogData.js index 57e89864a2..24818484a3 100644 --- a/src/core_modules/capture-core/components/WidgetsChangelog/common/utils/getSubValueForChangelogData.js +++ b/src/core_modules/capture-core/components/WidgetsChangelog/common/utils/getSubValueForChangelogData.js @@ -63,22 +63,28 @@ const buildTEAUrlByElementType: {| }: SubValueTEAProps) => { const { teiId, value } = trackedEntity; if (!value) return null; - const { id, displayName: name } = await querySingleResource({ resource: `fileResources/${value}` }); + try { + const { id, displayName: name } = await querySingleResource({ resource: `fileResources/${value}` }); - if (!latestValue) { - return name; - } + if (!latestValue) { + return name; + } - return { - id, - name, - url: `${absoluteApiPath}/tracker/trackedEntities/${teiId}/attributes/${attributeId}/image?program=${programId}`, - previewUrl: `${absoluteApiPath}/tracker/trackedEntities/${teiId}/attributes/${attributeId}/image?program=${programId}&dimension=small`, - }; + return { + id, + name, + url: `${absoluteApiPath}/tracker/trackedEntities/${teiId}/attributes/${attributeId}/image?program=${programId}`, + previewUrl: `${absoluteApiPath}/tracker/trackedEntities/${teiId}/attributes/${attributeId}/image?program=${programId}&dimension=small`, + }; + } catch (error) { + log.error( + errorCreator('Error fetching image resource')({ error }), + ); + return null; + } }, }; - const buildDataElementUrlByElementType: {| [string]: Function, |} = { @@ -86,34 +92,48 @@ const buildDataElementUrlByElementType: {| const { id: dataElementId, value } = dataElement; if (!value) return null; - const { id, displayName: name } = await querySingleResource({ resource: `fileResources/${value}` }); + try { + const { id, displayName: name } = await querySingleResource({ resource: `fileResources/${value}` }); - if (!latestValue) { - return name; - } + if (!latestValue) { + return name; + } - return { - id, - name, - url: `${absoluteApiPath}/tracker/events/${eventId}/dataValues/${dataElementId}/file`, - }; + return { + id, + name, + url: `${absoluteApiPath}/tracker/events/${eventId}/dataValues/${dataElementId}/file`, + }; + } catch (error) { + log.error( + errorCreator('Error fetching file resource')({ error }), + ); + return null; + } }, [dataElementTypes.IMAGE]: async ({ dataElement, querySingleResource, eventId, absoluteApiPath, latestValue }: SubValuesDataElementProps) => { const { id: dataElementId, value } = dataElement; if (!value) return null; - const { id, displayName: name } = await querySingleResource({ resource: `fileResources/${value}` }); + try { + const { id, displayName: name } = await querySingleResource({ resource: `fileResources/${value}` }); - if (!latestValue) { - return name; - } + if (!latestValue) { + return name; + } - return { - id, - name, - url: `${absoluteApiPath}/tracker/events/${eventId}/dataValues/${dataElementId}/image`, - previewUrl: `${absoluteApiPath}/tracker/events/${eventId}/dataValues/${dataElementId}/image?dimension=small`, - }; + return { + id, + name, + url: `${absoluteApiPath}/tracker/events/${eventId}/dataValues/${dataElementId}/image`, + previewUrl: `${absoluteApiPath}/tracker/events/${eventId}/dataValues/${dataElementId}/image?dimension=small`, + }; + } catch (error) { + log.error( + errorCreator('Error fetching image resource')({ error }), + ); + return null; + } }, };