From 6bd3ed72dd32fd31ea1606b7e893ccbaaf9acd40 Mon Sep 17 00:00:00 2001 From: Charles Gagnon Date: Tue, 18 Feb 2020 10:48:39 -0800 Subject: [PATCH] Display azure resource fetch errors as warnings (#9158) --- .../azurecore/src/azureResource/commands.ts | 27 ++++++++---- .../src/ui/modelViewUtils.ts | 42 +++++++++++++++---- 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/extensions/azurecore/src/azureResource/commands.ts b/extensions/azurecore/src/azureResource/commands.ts index d68da5bd83..c4fff0658e 100644 --- a/extensions/azurecore/src/azureResource/commands.ts +++ b/extensions/azurecore/src/azureResource/commands.ts @@ -28,7 +28,7 @@ export function registerAzureResourceCommands(appContext: AppContext, tree: Azur appContext.apiWrapper.registerCommand('azure.accounts.getSubscriptions', async (account?: azdata.Account, ignoreErrors: boolean = false): Promise => { const result: GetSubscriptionsResult = { subscriptions: [], errors: [] }; if (!account?.properties?.tenants || !isArray(account.properties.tenants)) { - const error = new Error('Invalid account'); + const error = new Error(localize('azure.accounts.getSubscriptions.invalidParamsError', "Invalid account")); if (!ignoreErrors) { throw error; } @@ -44,11 +44,15 @@ export function registerAzureResourceCommands(appContext: AppContext, tree: Azur result.subscriptions.push(...await subscriptionService.getSubscriptions(account, new TokenCredentials(token, tokenType))); } catch (err) { - console.warn(`Error fetching subscriptions for account ${account.displayInfo.displayName} tenant ${tenant.id} : ${err}`); + const error = new Error(localize('azure.accounts.getSubscriptions.queryError', "Error fetching subscriptions for account {0} tenant {1} : {2}", + account.displayInfo.displayName, + tenant.id, + err instanceof Error ? err.message : err)); + console.warn(error); if (!ignoreErrors) { - throw err; + throw error; } - result.errors.push(err); + result.errors.push(error); } return Promise.resolve(); })); @@ -58,7 +62,7 @@ export function registerAzureResourceCommands(appContext: AppContext, tree: Azur appContext.apiWrapper.registerCommand('azure.accounts.getResourceGroups', async (account?: azdata.Account, subscription?: azureResource.AzureResourceSubscription, ignoreErrors: boolean = false): Promise => { const result: GetResourceGroupsResult = { resourceGroups: [], errors: [] }; if (!account?.properties?.tenants || !isArray(account.properties.tenants) || !subscription) { - const error = new Error('Invalid account or subscription'); + const error = new Error(localize('azure.accounts.getResourceGroups.invalidParamsError', "Invalid account or subscription")); if (!ignoreErrors) { throw error; } @@ -74,11 +78,18 @@ export function registerAzureResourceCommands(appContext: AppContext, tree: Azur result.resourceGroups.push(...await service.getResources(subscription, new TokenCredentials(token, tokenType))); } catch (err) { - console.warn(`Error fetching resource groups for account ${account.displayInfo.displayName} (${account.displayInfo.userId}) subscription ${subscription.id} (${subscription.name}) tenant ${tenant.id} : ${err}`); + const error = new Error(localize('azure.accounts.getResourceGroups.queryError', "Error fetching resource groups for account {0} ({1}) subscription {2} ({3}) tenant {4} : {5}", + account.displayInfo.displayName, + account.displayInfo.userId, + subscription.id, + subscription.name, + tenant.id, + err instanceof Error ? err.message : err)); + console.warn(error); if (!ignoreErrors) { - throw err; + throw error; } - result.errors.push(err); + result.errors.push(error); } return Promise.resolve(); })); diff --git a/extensions/resource-deployment/src/ui/modelViewUtils.ts b/extensions/resource-deployment/src/ui/modelViewUtils.ts index 8a1d2c74dd..fc6708bd7f 100644 --- a/extensions/resource-deployment/src/ui/modelViewUtils.ts +++ b/extensions/resource-deployment/src/ui/modelViewUtils.ts @@ -12,6 +12,7 @@ import { getDateTimeString } from '../utils'; import { azureResource } from '../../../azurecore/src/azureResource/azure-resource'; import * as azurecore from '../../../azurecore/src/azurecore'; import * as loc from '../localizedConstants'; +import { EOL } from 'os'; const localize = nls.loadMessageBundle(); @@ -503,22 +504,33 @@ function handleSelectedAccountChanged( ): void { subscriptionValueToSubscriptionMap.clear(); subscriptionDropdown.values = []; - handleSelectedSubscriptionChanged(selectedAccount, undefined, resourceGroupDropdown); + handleSelectedSubscriptionChanged(context, selectedAccount, undefined, resourceGroupDropdown); if (selectedAccount) { if (locationDropdown.values && locationDropdown.values.length === 0) { locationDropdown.values = context.fieldInfo.locations; } } else { locationDropdown.values = []; + return; } - vscode.commands.executeCommand('azure.accounts.getSubscriptions', selectedAccount, true /*ignoreErrors*/).then(response => { - subscriptionDropdown.values = (response).subscriptions.map(subscription => { + vscode.commands.executeCommand('azure.accounts.getSubscriptions', selectedAccount, true /*ignoreErrors*/).then(response => { + if (!response) { + return; + } + if (response.errors.length > 0) { + context.container.message = { + text: response.errors.join(EOL) || '', + description: '', + level: azdata.window.MessageLevel.Warning + }; + } + subscriptionDropdown.values = response.subscriptions.map(subscription => { const displayName = `${subscription.name} (${subscription.id})`; subscriptionValueToSubscriptionMap.set(displayName, subscription); return displayName; }).sort((a: string, b: string) => a.toLocaleLowerCase().localeCompare(b.toLocaleLowerCase())); const selectedSubscription = subscriptionDropdown.values.length > 0 ? subscriptionValueToSubscriptionMap.get(subscriptionDropdown.values[0]) : undefined; - handleSelectedSubscriptionChanged(selectedAccount, selectedSubscription, resourceGroupDropdown); + handleSelectedSubscriptionChanged(context, selectedAccount, selectedSubscription, resourceGroupDropdown); }, err => { vscode.window.showErrorMessage(localize('azure.accounts.unexpectedSubscriptionsError', "Unexpected error fetching subscriptions for account {0} ({1}): {2}", selectedAccount?.displayInfo.displayName, selectedAccount?.key.accountId, err.message)); }); } @@ -545,15 +557,29 @@ function createAzureResourceGroupsDropdown( subscriptionDropdown.onValueChanged(selectedItem => { const selectedAccount = !accountDropdown || !accountDropdown.value ? undefined : accountValueToAccountMap.get(accountDropdown.value.toString()); const selectedSubscription = subscriptionValueToSubscriptionMap.get(selectedItem.selected); - handleSelectedSubscriptionChanged(selectedAccount, selectedSubscription, resourceGroupDropdown); + handleSelectedSubscriptionChanged(context, selectedAccount, selectedSubscription, resourceGroupDropdown); }); return resourceGroupDropdown; } -function handleSelectedSubscriptionChanged(selectedAccount: azdata.Account | undefined, selectedSubscription: azureResource.AzureResourceSubscription | undefined, resourceGroupDropdown: azdata.DropDownComponent): void { +function handleSelectedSubscriptionChanged(context: AzureAccountFieldContext, selectedAccount: azdata.Account | undefined, selectedSubscription: azureResource.AzureResourceSubscription | undefined, resourceGroupDropdown: azdata.DropDownComponent): void { resourceGroupDropdown.values = []; - vscode.commands.executeCommand('azure.accounts.getResourceGroups', selectedAccount, selectedSubscription, true /*ignoreErrors*/).then(response => { - resourceGroupDropdown.values = (response).resourceGroups.map(resourceGroup => resourceGroup.name).sort((a: string, b: string) => a.toLocaleLowerCase().localeCompare(b.toLocaleLowerCase())); + if (!selectedAccount || !selectedSubscription) { + // Don't need to execute command if we don't have both an account and subscription selected + return; + } + vscode.commands.executeCommand('azure.accounts.getResourceGroups', selectedAccount, selectedSubscription, true /*ignoreErrors*/).then(response => { + if (!response) { + return; + } + if (response.errors.length > 0) { + context.container.message = { + text: response.errors.join(EOL) || '', + description: '', + level: azdata.window.MessageLevel.Warning + }; + } + resourceGroupDropdown.values = response.resourceGroups.map(resourceGroup => resourceGroup.name).sort((a: string, b: string) => a.toLocaleLowerCase().localeCompare(b.toLocaleLowerCase())); }, err => { vscode.window.showErrorMessage(localize('azure.accounts.unexpectedResourceGroupsError', "Unexpected error fetching resource groups for subscription {0} ({1}): {2}", selectedSubscription?.name, selectedSubscription?.id, err.message)); }); }