Skip to content

Commit 36dc3ed

Browse files
author
Veetaha
committed
vscode: added error handling to download file streams
1 parent 759100f commit 36dc3ed

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

editors/code/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

editors/code/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"jsonc-parser": "^2.1.0",
2828
"node-fetch": "^2.6.0",
2929
"throttle-debounce": "^2.1.0",
30+
"ts-nested-error": "^1.1.3",
3031
"vscode-languageclient": "^6.1.0"
3132
},
3233
"devDependencies": {

editors/code/src/installation/download_file.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import fetch from "node-fetch";
22
import * as fs from "fs";
33
import { strict as assert } from "assert";
4+
import { NestedError } from "ts-nested-error";
5+
6+
class DownloadFileError extends NestedError {}
47

58
/**
69
* Downloads file from `url` and stores it at `destFilePath` with `destFilePermissions`.
@@ -14,13 +17,13 @@ export async function downloadFile(
1417
destFilePermissions: number,
1518
onProgress: (readBytes: number, totalBytes: number) => void
1619
): Promise<void> {
17-
const res = await fetch(url);
20+
const res = await fetch(url).catch(DownloadFileError.rethrow("Failed at initial fetch"));
1821

1922
if (!res.ok) {
2023
console.log("Error", res.status, "while downloading file from", url);
2124
console.dir({ body: await res.text(), headers: res.headers }, { depth: 3 });
2225

23-
throw new Error(`Got response ${res.status} when trying to download a file`);
26+
throw new DownloadFileError(`Got response ${res.status}`);
2427
}
2528

2629
const totalBytes = Number(res.headers.get('content-length'));
@@ -30,15 +33,21 @@ export async function downloadFile(
3033

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

36+
// Here reject() may be called 2 times. As per ECMAScript standard, 2-d call is ignored
37+
// https://tc39.es/ecma262/#sec-promise-reject-functions
38+
3339
return new Promise<void>((resolve, reject) => res.body
3440
.on("data", (chunk: Buffer) => {
3541
readBytes += chunk.length;
3642
onProgress(readBytes, totalBytes);
3743
})
38-
.on("error", reject)
39-
.pipe(fs
40-
.createWriteStream(destFilePath, { mode: destFilePermissions })
41-
.on("close", resolve)
42-
)
44+
.on("error", err => reject(
45+
new DownloadFileError(`Read-stream error, read bytes: ${readBytes}`, err)
46+
))
47+
.pipe(fs.createWriteStream(destFilePath, { mode: destFilePermissions }))
48+
.on("error", err => reject(
49+
new DownloadFileError(`Write-stream error, read bytes: ${readBytes}`, err)
50+
))
51+
.on("close", resolve)
4352
);
4453
}

editors/code/src/installation/language_server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ export async function ensureLanguageServerBinary(
104104
`GitHub repository: ${err.message}`
105105
);
106106

107+
console.error(err);
108+
107109
dns.resolve('example.com').then(
108110
addrs => console.log("DNS resolution for example.com was successful", addrs),
109111
err => {

0 commit comments

Comments
 (0)