diff options
author | Veetaha <[email protected]> | 2020-02-11 21:58:20 +0000 |
---|---|---|
committer | Veetaha <[email protected]> | 2020-02-11 21:58:48 +0000 |
commit | 36dc3edb7a73e0d60ba2a3e589d1ae76c27f9d9d (patch) | |
tree | cdea9366cb5ecd415830524e61fad3195f71bd84 /editors/code/src | |
parent | 759100fb0dcb41518f2a593dae5de5bbedd07776 (diff) |
vscode: added error handling to download file streams
Diffstat (limited to 'editors/code/src')
-rw-r--r-- | editors/code/src/installation/download_file.ts | 23 | ||||
-rw-r--r-- | editors/code/src/installation/language_server.ts | 2 |
2 files changed, 18 insertions, 7 deletions
diff --git a/editors/code/src/installation/download_file.ts b/editors/code/src/installation/download_file.ts index f1f9f4a25..71700ec8a 100644 --- a/editors/code/src/installation/download_file.ts +++ b/editors/code/src/installation/download_file.ts | |||
@@ -1,6 +1,9 @@ | |||
1 | import fetch from "node-fetch"; | 1 | import fetch from "node-fetch"; |
2 | import * as fs from "fs"; | 2 | import * as fs from "fs"; |
3 | import { strict as assert } from "assert"; | 3 | import { strict as assert } from "assert"; |
4 | import { NestedError } from "ts-nested-error"; | ||
5 | |||
6 | class DownloadFileError extends NestedError {} | ||
4 | 7 | ||
5 | /** | 8 | /** |
6 | * Downloads file from `url` and stores it at `destFilePath` with `destFilePermissions`. | 9 | * Downloads file from `url` and stores it at `destFilePath` with `destFilePermissions`. |
@@ -14,13 +17,13 @@ export async function downloadFile( | |||
14 | destFilePermissions: number, | 17 | destFilePermissions: number, |
15 | onProgress: (readBytes: number, totalBytes: number) => void | 18 | onProgress: (readBytes: number, totalBytes: number) => void |
16 | ): Promise<void> { | 19 | ): Promise<void> { |
17 | const res = await fetch(url); | 20 | const res = await fetch(url).catch(DownloadFileError.rethrow("Failed at initial fetch")); |
18 | 21 | ||
19 | if (!res.ok) { | 22 | if (!res.ok) { |
20 | console.log("Error", res.status, "while downloading file from", url); | 23 | console.log("Error", res.status, "while downloading file from", url); |
21 | console.dir({ body: await res.text(), headers: res.headers }, { depth: 3 }); | 24 | console.dir({ body: await res.text(), headers: res.headers }, { depth: 3 }); |
22 | 25 | ||
23 | throw new Error(`Got response ${res.status} when trying to download a file`); | 26 | throw new DownloadFileError(`Got response ${res.status}`); |
24 | } | 27 | } |
25 | 28 | ||
26 | const totalBytes = Number(res.headers.get('content-length')); | 29 | const totalBytes = Number(res.headers.get('content-length')); |
@@ -30,15 +33,21 @@ export async function downloadFile( | |||
30 | 33 | ||
31 | console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath); | 34 | console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath); |
32 | 35 | ||
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 | |||
33 | return new Promise<void>((resolve, reject) => res.body | 39 | return new Promise<void>((resolve, reject) => res.body |
34 | .on("data", (chunk: Buffer) => { | 40 | .on("data", (chunk: Buffer) => { |
35 | readBytes += chunk.length; | 41 | readBytes += chunk.length; |
36 | onProgress(readBytes, totalBytes); | 42 | onProgress(readBytes, totalBytes); |
37 | }) | 43 | }) |
38 | .on("error", reject) | 44 | .on("error", err => reject( |
39 | .pipe(fs | 45 | new DownloadFileError(`Read-stream error, read bytes: ${readBytes}`, err) |
40 | .createWriteStream(destFilePath, { mode: destFilePermissions }) | 46 | )) |
41 | .on("close", resolve) | 47 | .pipe(fs.createWriteStream(destFilePath, { mode: destFilePermissions })) |
42 | ) | 48 | .on("error", err => reject( |
49 | new DownloadFileError(`Write-stream error, read bytes: ${readBytes}`, err) | ||
50 | )) | ||
51 | .on("close", resolve) | ||
43 | ); | 52 | ); |
44 | } | 53 | } |
diff --git a/editors/code/src/installation/language_server.ts b/editors/code/src/installation/language_server.ts index 52c5cbe7d..4797c3f01 100644 --- a/editors/code/src/installation/language_server.ts +++ b/editors/code/src/installation/language_server.ts | |||
@@ -104,6 +104,8 @@ export async function ensureLanguageServerBinary( | |||
104 | `GitHub repository: ${err.message}` | 104 | `GitHub repository: ${err.message}` |
105 | ); | 105 | ); |
106 | 106 | ||
107 | console.error(err); | ||
108 | |||
107 | dns.resolve('example.com').then( | 109 | dns.resolve('example.com').then( |
108 | addrs => console.log("DNS resolution for example.com was successful", addrs), | 110 | addrs => console.log("DNS resolution for example.com was successful", addrs), |
109 | err => { | 111 | err => { |