mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-09 17:52:34 -05:00
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:
@@ -19,40 +19,71 @@ import { AzureResourceAccountTreeNode } from './tree/accountTreeNode';
|
||||
import { IAzureResourceSubscriptionService, IAzureResourceSubscriptionFilterService } from '../azureResource/interfaces';
|
||||
import { AzureResourceServiceNames } from './constants';
|
||||
import { AzureResourceGroupService } from './providers/resourceGroup/resourceGroupService';
|
||||
import { GetSubscriptionsResult, GetResourceGroupsResult } from '../azurecore';
|
||||
import { isArray } from 'util';
|
||||
|
||||
export function registerAzureResourceCommands(appContext: AppContext, tree: AzureResourceTreeProvider): void {
|
||||
|
||||
// Resource Management commands
|
||||
appContext.apiWrapper.registerCommand('azure.accounts.getSubscriptions', async (account?: azdata.Account): Promise<azureResource.AzureResourceSubscription[]> => {
|
||||
if (!account) {
|
||||
return [];
|
||||
appContext.apiWrapper.registerCommand('azure.accounts.getSubscriptions', async (account?: azdata.Account, ignoreErrors: boolean = false): Promise<GetSubscriptionsResult> => {
|
||||
const result: GetSubscriptionsResult = { subscriptions: [], errors: [] };
|
||||
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 tokens = await appContext.apiWrapper.getSecurityToken(account, azdata.AzureResource.ResourceManagement);
|
||||
for (const tenant of account.properties.tenants) {
|
||||
const token = tokens[tenant.id].token;
|
||||
const tokenType = tokens[tenant.id].tokenType;
|
||||
await Promise.all(account.properties.tenants.map(async (tenant: { id: string | number; }) => {
|
||||
try {
|
||||
const token = tokens[tenant.id].token;
|
||||
const tokenType = tokens[tenant.id].tokenType;
|
||||
|
||||
subscriptions.push(...await subscriptionService.getSubscriptions(account, new TokenCredentials(token, tokenType)));
|
||||
}
|
||||
return subscriptions;
|
||||
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}`);
|
||||
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[]> => {
|
||||
if (!account || !subscription) {
|
||||
return [];
|
||||
appContext.apiWrapper.registerCommand('azure.accounts.getResourceGroups', async (account?: azdata.Account, subscription?: azureResource.AzureResourceSubscription, ignoreErrors: boolean = false): Promise<GetResourceGroupsResult> => {
|
||||
const result: GetResourceGroupsResult = { resourceGroups: [], errors: [] };
|
||||
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 resourceGroups: azureResource.AzureResourceResourceGroup[] = [];
|
||||
for (const tenant of account.properties.tenants) {
|
||||
const tokens = await appContext.apiWrapper.getSecurityToken(account, azdata.AzureResource.ResourceManagement);
|
||||
const token = tokens[tenant.id].token;
|
||||
const tokenType = tokens[tenant.id].tokenType;
|
||||
await Promise.all(account.properties.tenants.map(async (tenant: { id: string | number; }) => {
|
||||
try {
|
||||
const tokens = await appContext.apiWrapper.getSecurityToken(account, azdata.AzureResource.ResourceManagement);
|
||||
const token = tokens[tenant.id].token;
|
||||
const tokenType = tokens[tenant.id].tokenType;
|
||||
|
||||
resourceGroups.push(...await service.getResources(subscription, new TokenCredentials(token, tokenType)));
|
||||
}
|
||||
return resourceGroups;
|
||||
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}`);
|
||||
if (!ignoreErrors) {
|
||||
throw err;
|
||||
}
|
||||
result.errors.push(err);
|
||||
}
|
||||
return Promise.resolve();
|
||||
}));
|
||||
|
||||
return result;
|
||||
});
|
||||
|
||||
// Resource Tree commands
|
||||
|
||||
@@ -85,7 +85,29 @@ export async function queryGraphResources<T extends GraphData>(resourceClient: R
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user