From 9a4952517150fdc2ff87fec7ae0241b265452fdc Mon Sep 17 00:00:00 2001 From: Thomas Weber Date: Sat, 14 May 2022 11:58:50 -0500 Subject: [PATCH] Improve error messages for failed project asset downloads --- src/packager/download-project.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/packager/download-project.js b/src/packager/download-project.js index 23a1b91..0a7988e 100644 --- a/src/packager/download-project.js +++ b/src/packager/download-project.js @@ -20,6 +20,17 @@ const flat = (array) => { return result; }; +const fetchAsArrayBuffer = (url) => fetch(url) + .then((r) => { + if (r.ok) { + return r.arrayBuffer(); + } + throw new Error(`Failed to fetch ${url}: status code ${r.status}`); + }) + .catch((err) => { + throw new Error(`Failed to fetch ${url}: ${err}`); + }); + const identifyProjectType = (projectData) => { if ('targets' in projectData) { return 'sb3'; @@ -135,8 +146,7 @@ const loadScratch2 = (projectData, progressTarget) => { progressTarget.dispatchEvent(new CustomEvent('asset-fetch', { detail: md5ext })); - return fetch(ASSET_HOST.replace('$path', md5ext)) - .then((res) => res.arrayBuffer()) + return fetchAsArrayBuffer(ASSET_HOST.replace('$path', md5ext)) .then((arrayBuffer) => { const path = `${id}.${getExtension(md5ext)}`; zip.file(path, arrayBuffer); @@ -196,8 +206,7 @@ const loadScratch3 = (projectData, progressTarget) => { progressTarget.dispatchEvent(new CustomEvent('asset-fetch', { detail: path })); - return fetch(ASSET_HOST.replace('$path', path)) - .then((request) => request.arrayBuffer()) + return fetchAsArrayBuffer(ASSET_HOST.replace('$path', path)) .then((buffer) => { zip.file(path, buffer); progressTarget.dispatchEvent(new CustomEvent('asset-fetched', {