vscode: add error handling to downloadFile()

This commit is contained in:
Veetaha 2020-02-11 02:14:04 +02:00
parent f8d6d6f23b
commit f2c66605c2

View file

@ -13,16 +13,23 @@ export async function downloadFile(
destFilePath: fs.PathLike,
onProgress: (readBytes: number, totalBytes: number) => void
): Promise<void> {
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<void>((resolve, reject) => response.body
return new Promise<void>((resolve, reject) => res.body
.on("data", (chunk: Buffer) => {
readBytes += chunk.length;
onProgress(readBytes, totalBytes);