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

@@ -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 {