From fc17340b50de02db0be4bea9654f2afd64963f87 Mon Sep 17 00:00:00 2001 From: Julien Deniau <1398469+jdeniau@users.noreply.github.com> Date: Mon, 26 Feb 2024 09:36:43 +0100 Subject: [PATCH] handle error with bundlephobia (#1990) --- resources/dist-stats.mjs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/resources/dist-stats.mjs b/resources/dist-stats.mjs index 986e533df..a63b087fd 100644 --- a/resources/dist-stats.mjs +++ b/resources/dist-stats.mjs @@ -26,9 +26,25 @@ let bundlephobaInfoCache; async function bundlephobaInfo(key) { if (!bundlephobaInfoCache) { - bundlephobaInfoCache = await fetch( - `https://bundlephobia.com/api/size?package=immutable@${VERIFY_AGAINST_VERSION}` - ).then(res => res.json()); + try { + const res = await fetch( + `https://bundlephobia.com/api/size?package=immutable@${VERIFY_AGAINST_VERSION}` + ); + + console.log(res.status); + + if (res.status !== 200) { + throw new Error( + `Unable to fetch bundlephobia in dist-stats.mjs. Status code is "${res.status}"` + ); + } + + bundlephobaInfoCache = await res.json(); + } catch (err) { + console.error(err.message); + + throw err; + } } return bundlephobaInfoCache[key];