From 36dc3edb7a73e0d60ba2a3e589d1ae76c27f9d9d Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 11 Feb 2020 23:58:20 +0200 Subject: vscode: added error handling to download file streams --- editors/code/src/installation/download_file.ts | 23 ++++++++++++++++------- editors/code/src/installation/language_server.ts | 2 ++ 2 files changed, 18 insertions(+), 7 deletions(-) (limited to 'editors/code/src') 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 @@ import fetch from "node-fetch"; import * as fs from "fs"; import { strict as assert } from "assert"; +import { NestedError } from "ts-nested-error"; + +class DownloadFileError extends NestedError {} /** * Downloads file from `url` and stores it at `destFilePath` with `destFilePermissions`. @@ -14,13 +17,13 @@ export async function downloadFile( destFilePermissions: number, onProgress: (readBytes: number, totalBytes: number) => void ): Promise { - const res = await fetch(url); + const res = await fetch(url).catch(DownloadFileError.rethrow("Failed at initial fetch")); 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`); + throw new DownloadFileError(`Got response ${res.status}`); } const totalBytes = Number(res.headers.get('content-length')); @@ -30,15 +33,21 @@ export async function downloadFile( console.log("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath); + // Here reject() may be called 2 times. As per ECMAScript standard, 2-d call is ignored + // https://tc39.es/ecma262/#sec-promise-reject-functions + return new Promise((resolve, reject) => res.body .on("data", (chunk: Buffer) => { readBytes += chunk.length; onProgress(readBytes, totalBytes); }) - .on("error", reject) - .pipe(fs - .createWriteStream(destFilePath, { mode: destFilePermissions }) - .on("close", resolve) - ) + .on("error", err => reject( + new DownloadFileError(`Read-stream error, read bytes: ${readBytes}`, err) + )) + .pipe(fs.createWriteStream(destFilePath, { mode: destFilePermissions })) + .on("error", err => reject( + new DownloadFileError(`Write-stream error, read bytes: ${readBytes}`, err) + )) + .on("close", resolve) ); } 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( `GitHub repository: ${err.message}` ); + console.error(err); + dns.resolve('example.com').then( addrs => console.log("DNS resolution for example.com was successful", addrs), err => { -- cgit v1.2.3