Improve azure resource API error handling (#9151)

* Improve azure resource API error handling

* Add ref path

* Add missed typings file and remove module references
This commit is contained in:
Charles Gagnon
2020-02-14 14:22:05 -08:00
committed by GitHub
parent dd5c0ce08f
commit 2d70ff7f4e
5 changed files with 102 additions and 27 deletions

View File

@@ -19,40 +19,71 @@ import { AzureResourceAccountTreeNode } from './tree/accountTreeNode';
import { IAzureResourceSubscriptionService, IAzureResourceSubscriptionFilterService } from '../azureResource/interfaces'; import { IAzureResourceSubscriptionService, IAzureResourceSubscriptionFilterService } from '../azureResource/interfaces';
import { AzureResourceServiceNames } from './constants'; import { AzureResourceServiceNames } from './constants';
import { AzureResourceGroupService } from './providers/resourceGroup/resourceGroupService'; import { AzureResourceGroupService } from './providers/resourceGroup/resourceGroupService';
import { GetSubscriptionsResult, GetResourceGroupsResult } from '../azurecore';
import { isArray } from 'util';
export function registerAzureResourceCommands(appContext: AppContext, tree: AzureResourceTreeProvider): void { export function registerAzureResourceCommands(appContext: AppContext, tree: AzureResourceTreeProvider): void {
// Resource Management commands // Resource Management commands
appContext.apiWrapper.registerCommand('azure.accounts.getSubscriptions', async (account?: azdata.Account): Promise<azureResource.AzureResourceSubscription[]> => { appContext.apiWrapper.registerCommand('azure.accounts.getSubscriptions', async (account?: azdata.Account, ignoreErrors: boolean = false): Promise<GetSubscriptionsResult> => {
if (!account) { const result: GetSubscriptionsResult = { subscriptions: [], errors: [] };
return []; if (!account?.properties?.tenants || !isArray(account.properties.tenants)) {
const error = new Error('Invalid account');
if (!ignoreErrors) {
throw error;
}
result.errors.push(error);
return result;
} }
const subscriptions = <azureResource.AzureResourceSubscription[]>[];
const subscriptionService = appContext.getService<IAzureResourceSubscriptionService>(AzureResourceServiceNames.subscriptionService); const subscriptionService = appContext.getService<IAzureResourceSubscriptionService>(AzureResourceServiceNames.subscriptionService);
const tokens = await appContext.apiWrapper.getSecurityToken(account, azdata.AzureResource.ResourceManagement); const tokens = await appContext.apiWrapper.getSecurityToken(account, azdata.AzureResource.ResourceManagement);
for (const tenant of account.properties.tenants) { await Promise.all(account.properties.tenants.map(async (tenant: { id: string | number; }) => {
const token = tokens[tenant.id].token; try {
const tokenType = tokens[tenant.id].tokenType; const token = tokens[tenant.id].token;
const tokenType = tokens[tenant.id].tokenType;
subscriptions.push(...await subscriptionService.getSubscriptions(account, new TokenCredentials(token, tokenType))); result.subscriptions.push(...await subscriptionService.getSubscriptions(account, new TokenCredentials(token, tokenType)));
} } catch (err) {
return subscriptions; console.warn(`Error fetching subscriptions for account ${account.displayInfo.displayName} tenant ${tenant.id} : ${err}`);
if (!ignoreErrors) {
throw err;
}
result.errors.push(err);
}
return Promise.resolve();
}));
return result;
}); });
appContext.apiWrapper.registerCommand('azure.accounts.getResourceGroups', async (account?: azdata.Account, subscription?: azureResource.AzureResourceSubscription): Promise<azureResource.AzureResourceResourceGroup[]> => { appContext.apiWrapper.registerCommand('azure.accounts.getResourceGroups', async (account?: azdata.Account, subscription?: azureResource.AzureResourceSubscription, ignoreErrors: boolean = false): Promise<GetResourceGroupsResult> => {
if (!account || !subscription) { const result: GetResourceGroupsResult = { resourceGroups: [], errors: [] };
return []; if (!account?.properties?.tenants || !isArray(account.properties.tenants) || !subscription) {
const error = new Error('Invalid account or subscription');
if (!ignoreErrors) {
throw error;
}
result.errors.push(error);
return result;
} }
const service = new AzureResourceGroupService(); const service = new AzureResourceGroupService();
const resourceGroups: azureResource.AzureResourceResourceGroup[] = []; await Promise.all(account.properties.tenants.map(async (tenant: { id: string | number; }) => {
for (const tenant of account.properties.tenants) { try {
const tokens = await appContext.apiWrapper.getSecurityToken(account, azdata.AzureResource.ResourceManagement); const tokens = await appContext.apiWrapper.getSecurityToken(account, azdata.AzureResource.ResourceManagement);
const token = tokens[tenant.id].token; const token = tokens[tenant.id].token;
const tokenType = tokens[tenant.id].tokenType; const tokenType = tokens[tenant.id].tokenType;
resourceGroups.push(...await service.getResources(subscription, new TokenCredentials(token, tokenType))); result.resourceGroups.push(...await service.getResources(subscription, new TokenCredentials(token, tokenType)));
} } catch (err) {
return resourceGroups; console.warn(`Error fetching resource groups for account ${account.displayInfo.displayName} (${account.displayInfo.userId}) subscription ${subscription.id} (${subscription.name}) tenant ${tenant.id} : ${err}`);
if (!ignoreErrors) {
throw err;
}
result.errors.push(err);
}
return Promise.resolve();
}));
return result;
}); });
// Resource Tree commands // Resource Tree commands

View File

@@ -85,7 +85,29 @@ export async function queryGraphResources<T extends GraphData>(resourceClient: R
await doQuery(response.skipToken); await doQuery(response.skipToken);
} }
}; };
await doQuery(); try {
await doQuery();
} catch (err) {
try {
if (err.response?.body) {
// The response object contains more useful error info than the error originally comes back with
const response = JSON.parse(err.response.body);
if (response.error?.details && Array.isArray(response.error.details) && response.error.details.length > 0) {
if (response.error.details[0].message) {
err.message = `${response.error.details[0].message}\n${err.message}`;
}
if (response.error.details[0].code) {
err.message = `${err.message} (${response.error.details[0].code})`;
}
}
}
} catch (err2) {
// Just log, we still want to throw the original error if something happens parsing the error
console.log(`Unexpected error while parsing error from querying resources : ${err2}`);
}
throw err;
}
return allResources; return allResources;
} }

22
extensions/azurecore/src/azurecore.d.ts vendored Normal file
View File

@@ -0,0 +1,22 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { azureResource } from './azureResource/azure-resource';
/**
* Covers defining what the azurecore extension exports to other extensions
*
* IMPORTANT: THIS IS NOT A HARD DEFINITION unlike vscode; therefore no enums or classes should be defined here
* (const enums get evaluated when typescript -> javascript so those are fine)
*/
export const enum extension {
name = 'Microsoft.azurecore'
}
export type GetSubscriptionsResult = { subscriptions: azureResource.AzureResourceSubscription[], errors: Error[] };
export type GetResourceGroupsResult = { resourceGroups: azureResource.AzureResourceResourceGroup[], errors: Error[] };

View File

@@ -6,5 +6,4 @@
/// <reference path='../../../../src/vs/vscode.d.ts'/> /// <reference path='../../../../src/vs/vscode.d.ts'/>
/// <reference path='../../../../src/sql/azdata.d.ts'/> /// <reference path='../../../../src/sql/azdata.d.ts'/>
/// <reference path='../../../../src/sql/azdata.proposed.d.ts'/> /// <reference path='../../../../src/sql/azdata.proposed.d.ts'/>
/// <reference path='../../../azurecore/src/azureResource/azure-resource.d.ts' />
/// <reference types='@types/node'/> /// <reference types='@types/node'/>

View File

@@ -10,6 +10,7 @@ import { DialogInfoBase, FieldType, FieldInfo, SectionInfo, LabelPosition, FontW
import { Model } from './model'; import { Model } from './model';
import { getDateTimeString } from '../utils'; import { getDateTimeString } from '../utils';
import { azureResource } from '../../../azurecore/src/azureResource/azure-resource'; import { azureResource } from '../../../azurecore/src/azureResource/azure-resource';
import * as azurecore from '../../../azurecore/src/azurecore';
import * as loc from '../localizedConstants'; import * as loc from '../localizedConstants';
const localize = nls.loadMessageBundle(); const localize = nls.loadMessageBundle();
@@ -510,8 +511,8 @@ function handleSelectedAccountChanged(
} else { } else {
locationDropdown.values = []; locationDropdown.values = [];
} }
vscode.commands.executeCommand('azure.accounts.getSubscriptions', selectedAccount).then(subscriptions => { vscode.commands.executeCommand('azure.accounts.getSubscriptions', selectedAccount, true /*ignoreErrors*/).then(response => {
subscriptionDropdown.values = (<azureResource.AzureResourceSubscription[]>subscriptions).map(subscription => { subscriptionDropdown.values = (<azurecore.GetSubscriptionsResult>response).subscriptions.map(subscription => {
const displayName = `${subscription.name} (${subscription.id})`; const displayName = `${subscription.name} (${subscription.id})`;
subscriptionValueToSubscriptionMap.set(displayName, subscription); subscriptionValueToSubscriptionMap.set(displayName, subscription);
return displayName; return displayName;
@@ -551,8 +552,8 @@ function createAzureResourceGroupsDropdown(
function handleSelectedSubscriptionChanged(selectedAccount: azdata.Account | undefined, selectedSubscription: azureResource.AzureResourceSubscription | undefined, resourceGroupDropdown: azdata.DropDownComponent): void { function handleSelectedSubscriptionChanged(selectedAccount: azdata.Account | undefined, selectedSubscription: azureResource.AzureResourceSubscription | undefined, resourceGroupDropdown: azdata.DropDownComponent): void {
resourceGroupDropdown.values = []; resourceGroupDropdown.values = [];
vscode.commands.executeCommand('azure.accounts.getResourceGroups', selectedAccount, selectedSubscription).then(resourceGroups => { vscode.commands.executeCommand('azure.accounts.getResourceGroups', selectedAccount, selectedSubscription, true /*ignoreErrors*/).then(response => {
resourceGroupDropdown.values = (<azureResource.AzureResourceSubscription[]>resourceGroups).map(resourceGroup => resourceGroup.name).sort((a: string, b: string) => a.toLocaleLowerCase().localeCompare(b.toLocaleLowerCase())); resourceGroupDropdown.values = (<azurecore.GetResourceGroupsResult>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)); }); }, err => { vscode.window.showErrorMessage(localize('azure.accounts.unexpectedResourceGroupsError', "Unexpected error fetching resource groups for subscription {0} ({1}): {2}", selectedSubscription?.name, selectedSubscription?.id, err.message)); });
} }