From f2c66605c2482e9e6b611b6e062ea475a42a620f Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 11 Feb 2020 02:14:04 +0200 Subject: vscode: add error handling to downloadFile() --- editors/code/src/installation/download_file.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'editors/code/src') diff --git a/editors/code/src/installation/download_file.ts b/editors/code/src/installation/download_file.ts index b51602ef9..9d37eaa0e 100644 --- a/editors/code/src/installation/download_file.ts +++ b/editors/code/src/installation/download_file.ts @@ -13,16 +13,23 @@ export async function downloadFile( destFilePath: fs.PathLike, onProgress: (readBytes: number, totalBytes: number) => void ): Promise { - const response = await fetch(url); + const res = await fetch(url); - const totalBytes = Number(response.headers.get('content-length')); + if (!res.ok) { + console.log("Error", res.status, "while downloading file from", url); + console.dir({ body: await res.text(), headers: res.headers }, { depth: 3 }); + + throw new Error(`Got response ${res.status} when trying to download a file`); + } + + const totalBytes = Number(res.headers.get('content-length')); assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol"); let readBytes = 0; console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath); - return new Promise((resolve, reject) => response.body + return new Promise((resolve, reject) => res.body .on("data", (chunk: Buffer) => { readBytes += chunk.length; onProgress(readBytes, totalBytes); -- cgit v1.2.3