Commit d6235367 authored by Lennard Sprong's avatar Lennard Sprong
Browse files

fix: Don't show error on startup when user is offline

parent c3b8ff4e
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -55,6 +55,12 @@ describe('validateAccounts', () => {
        error = new FetchError(createFakePartial<Response>({ status: 500 }), 'resource name');
      });

      it('ignores the error in quiet mode', async () => {
        mockedAccounts = [patAccount];
        await validateAccounts(true);
        expect(vscode.window.showErrorMessage).not.toHaveBeenCalled();
      });

      it('Show error and offer removing the account', async () => {
        mockedAccounts = [patAccount];
        await validateAccounts();
+12 −8
Original line number Diff line number Diff line
@@ -14,10 +14,11 @@ const IGNORE = 'Ignore Error';
/** ignored accounts are stored in memory and so after extension restarts, user needs to ignore them again */
const ignoredAccounts: string[] = [];

type AccountStatus = 'valid' | 'ignored' | 'invalid';
type AccountStatus = 'valid' | 'offline' | 'ignored' | 'invalid';

const validateAccount = async (
  account: Account,
  silentOffline: boolean,
): Promise<{ account: Account; status: AccountStatus }> => {
  const service = new RefreshingGitLabService(account);
  try {
@@ -42,11 +43,13 @@ const validateAccount = async (
      }
      return { account, status: 'invalid' };
    }

    const message = `Account validation for username ${account.username} on instance ${account.instanceUrl} failed. The extension can't connect to the instance.`;
    log.error(message, e);
    if (!silentOffline) {
      const DELETE_ACCOUNT = 'Delete Account';
      const SHOW_LOGS = 'Show Logs';
    const message = `Account validation for username ${account.username} on instance ${account.instanceUrl} failed. The extension can't connect to the instance.`;

    log.error(message, e);
      const response = await vscode.window.showErrorMessage(message, DELETE_ACCOUNT, SHOW_LOGS);
      switch (response) {
        case DELETE_ACCOUNT:
@@ -58,19 +61,20 @@ const validateAccount = async (
        default:
          break;
      }
    return { account, status: 'invalid' };
    }
    return { account, status: 'offline' };
  }
};

/**
 * command to validate that accounts don't have expired tokens
 * @param showOnlyErrors is used when we invoke the command during extension startup and we don't want to spam user with success messages
 * @param quiet is used when we invoke the command during extension startup and we don't want to spam user with success messages
 */
export const validateAccounts = async (showOnlyErrors = false) => {
export const validateAccounts = async (quiet = false) => {
  const accounts = accountService.getAllAccounts();
  const accountsWithStatus = await Promise.all(accounts.map(validateAccount));
  const accountsWithStatus = await Promise.all(accounts.map(acc => validateAccount(acc, quiet)));

  if (showOnlyErrors) {
  if (quiet) {
    return;
  }