Null & Error handling in Azure core (#22259)

This commit is contained in:
Cheena Malhotra
2023-03-09 14:34:39 -08:00
committed by GitHub
parent 0bbc290790
commit f51fe75397
2 changed files with 19 additions and 14 deletions

View File

@@ -139,11 +139,8 @@ export abstract class AzureAuth implements vscode.Disposable {
if (ex instanceof AzureAuthError) {
if (loginComplete) {
loginComplete.reject(ex);
Logger.error(ex);
} else {
void vscode.window.showErrorMessage(ex.message);
Logger.error(ex.originalMessageAndException);
}
Logger.error(ex.originalMessageAndException);
} else {
const message = ex.errorMessage || ex.message;
if (message) {
@@ -155,10 +152,11 @@ export abstract class AzureAuth implements vscode.Disposable {
};
}
Logger.error(ex);
}
return {
canceled: false
canceled: false,
errorCode: ex.errorCode,
errorMessage: ex.errorMessage || ex.message
};
}
}
@@ -476,7 +474,12 @@ export abstract class AzureAuth implements vscode.Disposable {
Logger.verbose('Fetching tenants with uri {0}', tenantUri);
let tenantList: string[] = [];
const tenantResponse = await this.makeGetRequest(tenantUri, token);
const tenants: Tenant[] = tenantResponse.data.value.map((tenantInfo: TenantResponse) => {
const data = tenantResponse.data;
if (data.error) {
Logger.error(`Error fetching tenants :${data.error.code} - ${data.error.message}`);
throw new Error(`${data.error.code} - ${data.error.message}`);
}
const tenants: Tenant[] = data.value.map((tenantInfo: TenantResponse) => {
if (tenantInfo.displayName) {
tenantList.push(tenantInfo.displayName);
} else {
@@ -501,7 +504,7 @@ export abstract class AzureAuth implements vscode.Disposable {
return tenants;
} catch (ex) {
Logger.error(`Error fetching tenants :${ex}`);
throw new Error('Error retrieving tenant information');
throw ex;
}
}

View File

@@ -28,7 +28,7 @@ export default class AccountStore implements IAccountStore {
return this.readFromMemento()
.then(accounts => {
// Determine if account exists and proceed accordingly
const match = accounts.findIndex(account => AccountStore.findAccountByKey(account.key, newAccount.key));
const match = accounts.findIndex(account => AccountStore.isSameAccountKey(account.key, newAccount.key));
return match < 0
? this.addToAccountList(accounts, newAccount)
: this.updateAccountList(accounts, newAccount.key, matchAccount => AccountStore.mergeAccounts(newAccount, matchAccount));
@@ -100,9 +100,11 @@ export default class AccountStore implements IAccountStore {
}
// PRIVATE METHODS /////////////////////////////////////////////////////
private static findAccountByKey(key1: azdata.AccountKey, key2: azdata.AccountKey): boolean {
private static isSameAccountKey(key1: azdata.AccountKey | undefined, key2: azdata.AccountKey | undefined): boolean {
// Provider ID and Account ID must match
return key1.providerId === key2.providerId && key1.accountId === key2.accountId;
return key1 && key2
? key1.providerId === key2.providerId && key1.accountId === key2.accountId
: false;
}
private static mergeAccounts(source: azdata.Account, target: azdata.Account): void {
@@ -135,7 +137,7 @@ export default class AccountStore implements IAccountStore {
private addToAccountList(accounts: azdata.Account[], accountToAdd: azdata.Account): AccountListOperationResult {
// Check if the entry already exists
const match = accounts.findIndex(account => AccountStore.findAccountByKey(account.key, accountToAdd.key));
const match = accounts.findIndex(account => AccountStore.isSameAccountKey(account.key, accountToAdd.key));
if (match >= 0) {
// Account already exists, we won't do anything
return {
@@ -160,7 +162,7 @@ export default class AccountStore implements IAccountStore {
private removeFromAccountList(accounts: azdata.Account[], accountToRemove: azdata.AccountKey): AccountListOperationResult {
// Check if the entry exists
const match = accounts.findIndex(account => AccountStore.findAccountByKey(account.key, accountToRemove));
const match = accounts.findIndex(account => AccountStore.isSameAccountKey(account.key, accountToRemove));
if (match >= 0) {
// Account exists, remove it from the account list
accounts.splice(match, 1);
@@ -177,7 +179,7 @@ export default class AccountStore implements IAccountStore {
private updateAccountList(accounts: azdata.Account[], accountToUpdate: azdata.AccountKey, updateOperation: (account: azdata.Account) => void): AccountListOperationResult {
// Check if the entry exists
const match = accounts.findIndex(account => AccountStore.findAccountByKey(account.key, accountToUpdate));
const match = accounts.findIndex(account => AccountStore.isSameAccountKey(account.key, accountToUpdate));
if (match < 0) {
// Account doesn't exist, we won't do anything
return {