Remove non-null assertions and refactor errors
This commit is contained in:
Родитель
084b4ade19
Коммит
66daa7dae3
|
@ -7,6 +7,7 @@ import * as fs from 'fs';
|
|||
import * as opn from 'opn';
|
||||
import * as path from 'path';
|
||||
import * as vscode from 'vscode';
|
||||
import * as errors from './errors';
|
||||
import * as FunctionsCli from './functions-cli';
|
||||
import { FunctionAppNode, INode } from './nodes';
|
||||
import * as TemplateFiles from './template-files';
|
||||
|
@ -28,7 +29,7 @@ export async function createFunction(outputChannel: vscode.OutputChannel): Promi
|
|||
let functionAppPath: string;
|
||||
const folders: vscode.WorkspaceFolder[] | undefined = vscode.workspace.workspaceFolders;
|
||||
if (!folders || folders.length === 0) {
|
||||
throw new util.NoWorkspaceError();
|
||||
throw new errors.NoWorkspaceError();
|
||||
} else if (folders.length === 1) {
|
||||
functionAppPath = folders[0].uri.fsPath;
|
||||
} else {
|
||||
|
@ -44,7 +45,7 @@ export async function createFunction(outputChannel: vscode.OutputChannel): Promi
|
|||
if (result === yes) {
|
||||
await FunctionsCli.createFunctionApp(outputChannel, functionAppPath);
|
||||
} else {
|
||||
throw new util.UserCancelledError();
|
||||
throw new errors.UserCancelledError();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
export class UserCancelledError extends Error { }
|
||||
|
||||
export class NoWorkspaceError extends Error {
|
||||
public message: string = 'You must have a workspace open to perform this operation.';
|
||||
}
|
||||
|
||||
export class ArgumentError extends Error {
|
||||
constructor(obj: object) {
|
||||
super(`Invalid ${obj.constructor.name}.`);
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@
|
|||
import * as vscode from 'vscode';
|
||||
import { AzureAccount } from './azure-account.api';
|
||||
import * as commands from './commands';
|
||||
import * as errors from './errors';
|
||||
import { AzureFunctionsExplorer } from './explorer';
|
||||
import { FunctionAppNode, INode } from './nodes';
|
||||
import { Reporter } from './telemetry';
|
||||
|
@ -16,8 +17,12 @@ import * as util from './util';
|
|||
export function activate(context: vscode.ExtensionContext): void {
|
||||
context.subscriptions.push(new Reporter(context));
|
||||
|
||||
const azureAccount: AzureAccount = vscode.extensions.getExtension<AzureAccount>('ms-vscode.azure-account')!.exports;
|
||||
if (azureAccount) {
|
||||
const azureAccountExtension: vscode.Extension<AzureAccount> | undefined = vscode.extensions.getExtension<AzureAccount>('ms-vscode.azure-account');
|
||||
if (!azureAccountExtension || !azureAccountExtension.exports) {
|
||||
vscode.window.showErrorMessage('The Azure Account Extension is required for the Azure Functions extension.');
|
||||
} else {
|
||||
const azureAccount: AzureAccount = azureAccountExtension.exports;
|
||||
|
||||
context.subscriptions.push(azureAccount.onFiltersChanged(() => explorer.refresh()));
|
||||
context.subscriptions.push(azureAccount.onStatusChanged(() => explorer.refresh()));
|
||||
|
||||
|
@ -34,8 +39,6 @@ export function activate(context: vscode.ExtensionContext): void {
|
|||
initAsyncCommand(context, 'azureFunctions.startFunctionApp', (node?: FunctionAppNode) => commands.startFunctionApp(outputChannel, node));
|
||||
initAsyncCommand(context, 'azureFunctions.stopFunctionApp', (node?: FunctionAppNode) => commands.stopFunctionApp(outputChannel, node));
|
||||
initAsyncCommand(context, 'azureFunctions.restartFunctionApp', (node?: FunctionAppNode) => commands.restartFunctionApp(outputChannel, node));
|
||||
} else {
|
||||
vscode.window.showErrorMessage('The Azure Account Extension is required for the Azure Functions extension.');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,7 +59,7 @@ function initAsyncCommand(context: vscode.ExtensionContext, commandId: string, c
|
|||
try {
|
||||
args.length === 0 ? await callback() : await callback(<INode>args[0]);
|
||||
} catch (error) {
|
||||
if (error instanceof util.UserCancelledError) {
|
||||
if (error instanceof errors.UserCancelledError) {
|
||||
result = 'Canceled';
|
||||
// Swallow the error so that it's not displayed to the user
|
||||
} else {
|
||||
|
|
35
src/nodes.ts
35
src/nodes.ts
|
@ -7,6 +7,7 @@ import * as path from 'path';
|
|||
import * as vscode from 'vscode';
|
||||
import { Site, WebAppCollection } from '../node_modules/azure-arm-website/lib/models';
|
||||
import { AzureResourceFilter } from './azure-account.api';
|
||||
import * as errors from './errors';
|
||||
import * as util from './util';
|
||||
|
||||
export interface INode extends vscode.TreeItem {
|
||||
|
@ -44,9 +45,13 @@ export class SubscriptionNode implements INode {
|
|||
private readonly subscriptionFilter: AzureResourceFilter;
|
||||
|
||||
constructor(subscriptionFilter: AzureResourceFilter) {
|
||||
if (!subscriptionFilter.subscription.displayName || !subscriptionFilter.subscription.subscriptionId) {
|
||||
throw new errors.ArgumentError(subscriptionFilter);
|
||||
}
|
||||
|
||||
this.subscriptionFilter = subscriptionFilter;
|
||||
this.label = subscriptionFilter.subscription.displayName!;
|
||||
this.id = subscriptionFilter.subscription.subscriptionId!;
|
||||
this.label = subscriptionFilter.subscription.displayName;
|
||||
this.id = subscriptionFilter.subscription.subscriptionId;
|
||||
this.tenantId = subscriptionFilter.session.tenantId;
|
||||
}
|
||||
|
||||
|
@ -61,8 +66,8 @@ export class SubscriptionNode implements INode {
|
|||
const webApps: WebAppCollection = await this.getWebSiteClient().webApps.list();
|
||||
|
||||
return webApps.filter((s: Site) => s.kind === 'functionapp')
|
||||
.sort((s1: Site, s2: Site) => s1.id!.localeCompare(s2.id!))
|
||||
.map((s: Site) => new FunctionAppNode(s, this));
|
||||
.map((s: Site) => new FunctionAppNode(s, this))
|
||||
.sort((f1: FunctionAppNode, f2: FunctionAppNode) => f1.id.localeCompare(f2.id));
|
||||
}
|
||||
|
||||
public getWebSiteClient(): WebSiteManagementClient {
|
||||
|
@ -76,14 +81,20 @@ export class FunctionAppNode implements INode {
|
|||
public readonly id: string;
|
||||
public readonly tenantId: string;
|
||||
|
||||
private readonly functionApp: Site;
|
||||
private readonly resourceGroup: string;
|
||||
private readonly name: string;
|
||||
private readonly subscriptionNode: SubscriptionNode;
|
||||
|
||||
constructor(functionApp: Site, subscriptionNode: SubscriptionNode) {
|
||||
this.functionApp = functionApp;
|
||||
if (!functionApp.id || !functionApp.resourceGroup || !functionApp.name) {
|
||||
throw new errors.ArgumentError(functionApp);
|
||||
}
|
||||
|
||||
this.id = functionApp.id;
|
||||
this.resourceGroup = functionApp.resourceGroup;
|
||||
this.name = functionApp.name;
|
||||
this.subscriptionNode = subscriptionNode;
|
||||
this.label = `${functionApp.name} (${this.functionApp.resourceGroup})`;
|
||||
this.id = functionApp.id!;
|
||||
this.label = `${functionApp.name} (${this.resourceGroup})`;
|
||||
this.tenantId = subscriptionNode.tenantId;
|
||||
}
|
||||
|
||||
|
@ -96,14 +107,14 @@ export class FunctionAppNode implements INode {
|
|||
|
||||
public async start(): Promise<void> {
|
||||
const client: WebSiteManagementClient = this.subscriptionNode.getWebSiteClient();
|
||||
await client.webApps.start(this.functionApp.resourceGroup!, this.functionApp.name!);
|
||||
await util.waitForFunctionAppState(client, this.functionApp.resourceGroup!, this.functionApp.name!, util.FunctionAppState.Running);
|
||||
await client.webApps.start(this.resourceGroup, this.name);
|
||||
await util.waitForFunctionAppState(client, this.resourceGroup, this.name, util.FunctionAppState.Running);
|
||||
}
|
||||
|
||||
public async stop(): Promise<void> {
|
||||
const client: WebSiteManagementClient = this.subscriptionNode.getWebSiteClient();
|
||||
await client.webApps.stop(this.functionApp.resourceGroup!, this.functionApp.name!);
|
||||
await util.waitForFunctionAppState(client, this.functionApp.resourceGroup!, this.functionApp.name!, util.FunctionAppState.Stopped);
|
||||
await client.webApps.stop(this.resourceGroup, this.name);
|
||||
await util.waitForFunctionAppState(client, this.resourceGroup, this.name, util.FunctionAppState.Stopped);
|
||||
}
|
||||
|
||||
public async restart(): Promise<void> {
|
||||
|
|
13
src/util.ts
13
src/util.ts
|
@ -7,6 +7,7 @@ import WebSiteManagementClient = require('azure-arm-website');
|
|||
import * as fs from 'fs';
|
||||
import * as vscode from 'vscode';
|
||||
import { Site } from '../node_modules/azure-arm-website/lib/models';
|
||||
import * as errors from './errors';
|
||||
import { reporter } from './telemetry';
|
||||
|
||||
export function sendTelemetry(eventName: string, properties?: { [key: string]: string; }, measures?: { [key: string]: number; }): void {
|
||||
|
@ -42,7 +43,7 @@ export async function showQuickPick(items: vscode.QuickPickItem[] | Thenable<vsc
|
|||
const result: vscode.QuickPickItem | undefined = await vscode.window.showQuickPick(items, options);
|
||||
|
||||
if (!result) {
|
||||
throw new UserCancelledError();
|
||||
throw new errors.UserCancelledError();
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
|
@ -58,7 +59,7 @@ export async function showInputBox(placeHolder: string, prompt: string, ignoreFo
|
|||
const result: string | undefined = await vscode.window.showInputBox(options);
|
||||
|
||||
if (!result) {
|
||||
throw new UserCancelledError();
|
||||
throw new errors.UserCancelledError();
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
|
@ -76,7 +77,7 @@ export async function showFolderDialog(): Promise<string> {
|
|||
const result: vscode.Uri[] | undefined = await vscode.window.showOpenDialog(options);
|
||||
|
||||
if (!result || result.length === 0) {
|
||||
throw new UserCancelledError();
|
||||
throw new errors.UserCancelledError();
|
||||
} else {
|
||||
return result[0].fsPath;
|
||||
}
|
||||
|
@ -124,9 +125,3 @@ export function writeToFile(path: string, data: string): Promise<void> {
|
|||
});
|
||||
});
|
||||
}
|
||||
|
||||
export class UserCancelledError extends Error { }
|
||||
|
||||
export class NoWorkspaceError extends Error {
|
||||
public message: string = 'You must have a workspace open to perform this operation.';
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче