From 852587960c276f1450a889bf4a4f68f5869cb94b Mon Sep 17 00:00:00 2001 From: Lucas Porto Date: Mon, 23 Sep 2024 17:19:10 -0300 Subject: [PATCH 1/2] fix: change related produts promise type --- vtex/loaders/legacy/relatedProductsLoader.ts | 21 +++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/vtex/loaders/legacy/relatedProductsLoader.ts b/vtex/loaders/legacy/relatedProductsLoader.ts index 67919d5b1..fecd3bdfc 100644 --- a/vtex/loaders/legacy/relatedProductsLoader.ts +++ b/vtex/loaders/legacy/relatedProductsLoader.ts @@ -102,11 +102,26 @@ async function loader( /** Batch fetches due to VTEX API limits */ const batchedIds = batch(relatedIds, 50); - const relatedProducts = await Promise.all( + + const relatedProductsResults = await Promise.allSettled( batchedIds.map((ids) => productList({ props: { similars: false, ids } }, req, ctx) ), - ).then((p) => p.flat().filter((x): x is Product => Boolean(x))); + ); + + const relatedProducts = relatedProductsResults + .filter( + (result): result is PromiseFulfilledResult => + result.status === "fulfilled", + ) + .flatMap((result) => result.value) + .filter((x): x is Product => Boolean(x)); + + relatedProductsResults + .filter((result) => result.status === "rejected") + .forEach((result) => + console.error("Error loading related products:", result.reason) + ); // Search API does not offer a way to filter out in stock products // This is a scape hatch @@ -153,4 +168,4 @@ export const cacheKey = (props: Props, req: Request, ctx: AppContext) => { return url.href; }; -export default loader; +export default loader; \ No newline at end of file From 4f2501ed485910e98c99df9914f5ebcd1c8acc61 Mon Sep 17 00:00:00 2001 From: Lucas Porto Date: Tue, 24 Sep 2024 15:23:34 -0300 Subject: [PATCH 2/2] chore: add error handling for one and all products --- vtex/loaders/legacy/relatedProductsLoader.ts | 21 ++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/vtex/loaders/legacy/relatedProductsLoader.ts b/vtex/loaders/legacy/relatedProductsLoader.ts index fecd3bdfc..2908d987d 100644 --- a/vtex/loaders/legacy/relatedProductsLoader.ts +++ b/vtex/loaders/legacy/relatedProductsLoader.ts @@ -119,12 +119,21 @@ async function loader( relatedProductsResults .filter((result) => result.status === "rejected") - .forEach((result) => - console.error("Error loading related products:", result.reason) - ); + .forEach((result, index) => { + console.error( + `Error loading related products for batch ${index}:`, + (result as PromiseRejectedResult).reason, + ); + }); + + const allPromisesFailed = relatedProductsResults.every( + (result) => result.status === "rejected", + ); + + if (allPromisesFailed) { + throw new Error("Failed to load related products for all batches."); + } - // Search API does not offer a way to filter out in stock products - // This is a scape hatch if (hideUnavailableItems && relatedProducts) { const inStock = (p: Product) => p.offers?.offers.find((o) => @@ -168,4 +177,4 @@ export const cacheKey = (props: Props, req: Request, ctx: AppContext) => { return url.href; }; -export default loader; \ No newline at end of file +export default loader;