提交 aa7721f4 编写于 作者: B Benjamin Pasero

debt - avoid Promise.reject() in async functions

上级 ad6011cb
......@@ -764,5 +764,5 @@ export async function retry<T>(task: ITask<Promise<T>>, delay: number, retries:
}
}
return Promise.reject(lastError);
throw lastError;
}
......@@ -641,25 +641,28 @@ export async function mkdirp(path: string, mode?: number, token?: CancellationTo
// ENOENT: a parent folder does not exist yet
if (error.code === 'ENOENT') {
return Promise.reject(error);
throw error;
}
// Any other error: check if folder exists and
// return normally in that case if its a folder
let targetIsFile = false;
try {
const fileStat = await stat(path);
if (!fileStat.isDirectory()) {
return Promise.reject(new Error(`'${path}' exists and is not a directory.`));
}
targetIsFile = !fileStat.isDirectory();
} catch (statError) {
throw error; // rethrow original error
throw error; // rethrow original error if stat fails
}
if (targetIsFile) {
throw new Error(`'${path}' exists and is not a directory.`);
}
}
};
// stop at root
if (path === dirname(path)) {
return Promise.resolve();
return;
}
try {
......@@ -680,7 +683,7 @@ export async function mkdirp(path: string, mode?: number, token?: CancellationTo
}
// Any other error
return Promise.reject(error);
throw error;
}
}
......
......@@ -253,7 +253,7 @@ suite('PFS', () => {
}
catch (error) {
assert.fail(error);
return Promise.reject(error);
throw error;
}
});
......
......@@ -102,7 +102,7 @@ export class OpenerService extends Disposable implements IOpenerService {
if (equalsIgnoreCase(scheme, Schemas.command)) {
// run command or bail out if command isn't known
if (!CommandsRegistry.getCommand(path)) {
return Promise.reject(`command '${path}' NOT known`);
throw new Error(`command '${path}' NOT known`);
}
// execute as command
let args: any = [];
......
......@@ -30,7 +30,7 @@ export class DownloadService implements IDownloadService {
await this.fileService.writeFile(target, context.stream);
} else {
const message = await asText(context);
return Promise.reject(new Error(`Expected 200, got back ${context.res.statusCode} instead.\n\n${message}`));
throw new Error(`Expected 200, got back ${context.res.statusCode} instead.\n\n${message}`);
}
}
}
......@@ -153,7 +153,7 @@ export class SettingsSynchroniser extends Disposable implements ISynchroniser {
if (this.hasErrors(content)) {
const error = new Error(localize('errorInvalidSettings', "Unable to sync settings. Please resolve conflicts without any errors/warnings and try again."));
this.logService.error(error);
return Promise.reject(error);
throw error;
}
let { fileContent, remoteUserData, hasLocalChanged, hasRemoteChanged } = await this.syncPreviewResultPromise;
......
......@@ -53,7 +53,7 @@ export class UserDataSyncService extends Disposable implements IUserDataSyncServ
throw new Error('Not enabled');
}
if (this.authTokenService.status === AuthTokenStatus.Inactive) {
return Promise.reject('Not Authenticated. Please sign in to start sync.');
throw new Error('Not Authenticated. Please sign in to start sync.');
}
for (const synchroniser of this.synchronisers) {
if (!await synchroniser.sync(_continue)) {
......
......@@ -29,7 +29,7 @@ export class UserDataSyncStoreService extends Disposable implements IUserDataSyn
async read(key: string, oldValue: IUserData | null): Promise<IUserData> {
if (!this.enabled) {
return Promise.reject(new Error('No settings sync store url configured.'));
throw new Error('No settings sync store url configured.');
}
const url = joinPath(URI.parse(this.productService.settingsSyncStoreUrl!), 'resource', key, 'latest').toString();
......@@ -59,7 +59,7 @@ export class UserDataSyncStoreService extends Disposable implements IUserDataSyn
async write(key: string, data: string, ref: string | null): Promise<string> {
if (!this.enabled) {
return Promise.reject(new Error('No settings sync store url configured.'));
throw new Error('No settings sync store url configured.');
}
const url = joinPath(URI.parse(this.productService.settingsSyncStoreUrl!), 'resource', key).toString();
......@@ -90,7 +90,7 @@ export class UserDataSyncStoreService extends Disposable implements IUserDataSyn
if (this.authTokenService.status !== AuthTokenStatus.Disabled) {
const authToken = await this.authTokenService.getToken();
if (!authToken) {
return Promise.reject(new Error('No Auth Token Available.'));
throw new Error('No Auth Token Available.');
}
options.headers = options.headers || {};
options.headers['authorization'] = `Bearer ${authToken}`;
......
......@@ -429,13 +429,13 @@ export class InMemoryBackupFileService implements IBackupFileService {
return Promise.resolve();
}
resolveBackupContent<T extends object>(backupResource: URI): Promise<IResolvedBackup<T>> {
async resolveBackupContent<T extends object>(backupResource: URI): Promise<IResolvedBackup<T>> {
const snapshot = this.backups.get(backupResource.toString());
if (snapshot) {
return Promise.resolve({ value: createTextBufferFactoryFromSnapshot(snapshot) });
return { value: createTextBufferFactoryFromSnapshot(snapshot) };
}
return Promise.reject('Unexpected backup resource to resolve');
throw new Error('Unexpected backup resource to resolve');
}
getWorkspaceFileBackups(): Promise<URI[]> {
......
......@@ -45,7 +45,7 @@ export class ProgressService extends Disposable implements IProgressService {
super();
}
withProgress<R = unknown>(options: IProgressOptions, task: (progress: IProgress<IProgressStep>) => Promise<R>, onDidCancel?: (choice?: number) => void): Promise<R> {
async withProgress<R = unknown>(options: IProgressOptions, task: (progress: IProgress<IProgressStep>) => Promise<R>, onDidCancel?: (choice?: number) => void): Promise<R> {
const { location } = options;
if (typeof location === 'string') {
if (this.viewletService.getProgressIndicator(location)) {
......@@ -56,7 +56,7 @@ export class ProgressService extends Disposable implements IProgressService {
return this.withPanelProgress(location, task, { ...options, location });
}
return Promise.reject(new Error(`Bad progress location: ${location}`));
throw new Error(`Bad progress location: ${location}`);
}
switch (location) {
......@@ -73,7 +73,7 @@ export class ProgressService extends Disposable implements IProgressService {
case ProgressLocation.Dialog:
return this.withDialogProgress(options, task, onDidCancel);
default:
return Promise.reject(new Error(`Bad progress location: ${location}`));
throw new Error(`Bad progress location: ${location}`);
}
}
......
......@@ -120,7 +120,7 @@ export abstract class AbstractWorkspaceEditingService implements IWorkspaceEditi
// Do not allow workspace folders with scheme different than the current remote scheme
const schemas = this.contextService.getWorkspace().folders.map(f => f.uri.scheme);
if (schemas.length && foldersToAdd.some(f => schemas.indexOf(f.uri.scheme) === -1)) {
return Promise.reject(new Error(nls.localize('differentSchemeRoots', "Workspace folders from different providers are not allowed in the same workspace.")));
throw new Error(nls.localize('differentSchemeRoots', "Workspace folders from different providers are not allowed in the same workspace."));
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册