Skip to content

Commit f2c6660

Browse files
author
Veetaha
committed
vscode: add error handling to downloadFile()
1 parent f8d6d6f commit f2c6660

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

editors/code/src/installation/download_file.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,23 @@ export async function downloadFile(
1313
destFilePath: fs.PathLike,
1414
onProgress: (readBytes: number, totalBytes: number) => void
1515
): Promise<void> {
16-
const response = await fetch(url);
16+
const res = await fetch(url);
1717

18-
const totalBytes = Number(response.headers.get('content-length'));
18+
if (!res.ok) {
19+
console.log("Error", res.status, "while downloading file from", url);
20+
console.dir({ body: await res.text(), headers: res.headers }, { depth: 3 });
21+
22+
throw new Error(`Got response ${res.status} when trying to download a file`);
23+
}
24+
25+
const totalBytes = Number(res.headers.get('content-length'));
1926
assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol");
2027

2128
let readBytes = 0;
2229

2330
console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath);
2431

25-
return new Promise<void>((resolve, reject) => response.body
32+
return new Promise<void>((resolve, reject) => res.body
2633
.on("data", (chunk: Buffer) => {
2734
readBytes += chunk.length;
2835
onProgress(readBytes, totalBytes);

0 commit comments

Comments
 (0)