mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
vscode: add error handling to downloadFile()
This commit is contained in:
parent
f8d6d6f23b
commit
f2c66605c2
1 changed files with 10 additions and 3 deletions
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue