When connecting a SSH FS, make errors reject the promise

pull/13/head
Kelvin Schoofs 7 years ago
parent 59b4f6af9b
commit 8659ec01d7

@ -4,10 +4,9 @@ import { parse as parseJsonc, ParseError } from 'jsonc-parser';
import * as path from 'path';
import { Client, ConnectConfig } from 'ssh2';
import * as vscode from 'vscode';
import { getSession as getPuttySession, PuttySession } from './putty';
import { getSession as getPuttySession } from './putty';
import SSHFileSystem, { EMPTY_FILE_SYSTEM } from './sshFileSystem';
import { toPromise } from './toPromise';
import { catchingPromise, toPromise } from './toPromise';
async function assertFs(man: Manager, uri: vscode.Uri) {
const fs = await man.getFs(uri);
@ -142,7 +141,7 @@ export class Manager implements vscode.FileSystemProvider, vscode.TreeDataProvid
if (promise) return promise;
// config = config || this.memento.get(`fs.config.${name}`);
config = config || this.loadConfigs().find(c => c.name === name);
promise = new Promise<SSHFileSystem>(async (resolve, reject) => {
promise = catchingPromise<SSHFileSystem>(async (resolve, reject) => {
if (!config) {
throw new Error(`A SSH filesystem with the name '${name}' doesn't exist`);
}
@ -228,6 +227,7 @@ export class Manager implements vscode.FileSystemProvider, vscode.TreeDataProvid
reject(e);
}
}).catch((e) => {
this.onDidChangeTreeDataEmitter.fire();
if (!e) {
delete this.creatingFileSystems[name];
this.commandDisconnect(name);

@ -5,3 +5,16 @@ export async function toPromise<T>(func: (cb: toPromiseCallback<T>) => void): Pr
func((err, res) => err ? reject(err) : resolve(res));
});
}
export async function catchingPromise<T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => any): Promise<T> {
return new Promise<T>((resolve, reject) => {
try {
const p = executor(resolve, reject);
if (p instanceof Promise) {
p.catch(reject);
}
} catch (e) {
reject(e);
}
});
}

Loading…
Cancel
Save