Skip to content

Commit

Permalink
fix: Cleanup error handler inside of getDataFromExternalSources
Browse files Browse the repository at this point in the history
  • Loading branch information
richtera committed Jan 16, 2024
1 parent 038d975 commit fde2e0f
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/lib/getDataFromExternalSources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,28 @@ export const getDataFromExternalSources = (
console.error(
`Value of key: ${dataEntry.name} (${dataEntry.value}) is string but valueContent is: ${schemaElement.valueContent}. Expected type should be object with url key.`,
);
return dataEntry;
return { ...dataEntry, value: null };
}

if (!dataEntry.value) {
return dataEntry;
return { ...dataEntry, value: null };
}

if (Array.isArray(dataEntry.value)) {
console.error(
`Value of key: ${dataEntry.name} (${dataEntry.value}) is string[] but valueContent is: ${schemaElement.valueContent}. Expected type should be object with url key.`,
);
return dataEntry;
return { ...dataEntry, value: null };
}

const urlDataWithHash = dataEntry.value; // Type URLDataWithHash

let receivedData;
const { url } = patchIPFSUrlsIfApplicable(urlDataWithHash, ipfsGateway);
try {
const { url } = patchIPFSUrlsIfApplicable(urlDataWithHash, ipfsGateway);

receivedData = await fetch(url).then(async (response) => {
if (!response.ok) {
return undefined;
throw new Error(response.statusText);
}
if (
urlDataWithHash.verification?.method ===
Expand All @@ -93,11 +92,12 @@ export const getDataFromExternalSources = (
if (isDataAuthentic(receivedData, urlDataWithHash.verification)) {
return { ...dataEntry, value: receivedData };
}
throw new Error('result did not correctly validate');
} catch (error: any) {
console.error(
`GET request to ${urlDataWithHash.url} did not correctly validate`,
error,
`GET request to ${urlDataWithHash.url} (resolved as ${url})`,
);
} catch (error) {
console.error(error, `GET request to ${urlDataWithHash.url} failed`);
}
// Invalid data
return { ...dataEntry, value: null };
Expand Down

0 comments on commit fde2e0f

Please sign in to comment.