Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update browser extension error handling and display (#2437)
Leendert de Borst036547af1ab9
diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.tsindex 4c1ea88a2..a3c7b9f56 100644--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts@@ -802,7 +802,7 @@ async function persistSyncErrorState(result: FullVaultSyncResult): Promise<void>const dedicatedError = result.requiresLogout || result.wasOffline;if (hasSyncError(result) && !dedicatedError) {- await storage.setItem(StorageKeys.LAST_SYNC_ERROR, { errorKey: result.errorKey, errorCode: result.errorCode, error: result.error });+ await storage.setItem(StorageKeys.LAST_SYNC_ERROR, { logoutReason: result.logoutReason, errorCode: result.errorCode, error: result.error });} else {await storage.removeItem(StorageKeys.LAST_SYNC_ERROR);}diff --git a/apps/browser-extension/src/entrypoints/popup/context/AppContext.tsx b/apps/browser-extension/src/entrypoints/popup/context/AppContext.tsxindex 94a771540..825c3ae9f 100644--- a/apps/browser-extension/src/entrypoints/popup/context/AppContext.tsx+++ b/apps/browser-extension/src/entrypoints/popup/context/AppContext.tsx@@ -80,8 +80,8 @@ export const AppProvider: React.FC<{ children: React.ReactNode }> = ({ children* Subscribe to logout events from WebApiService.*/useEffect(() => {- const unsubscribe = logoutEventEmitter.subscribe(async (errorKey: string) => {- await logout(t(errorKey));+ const unsubscribe = logoutEventEmitter.subscribe(async (messageKey) => {+ await logout(t(messageKey));});return unsubscribe;diff --git a/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsx b/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsxindex cd7bfa917..961ff2b96 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsx@@ -1,8 +1,7 @@-import { apiErrorCodeOf } from '@aliasvault/client/api/errors/ApiRequestError';-import { hasErrorCode, getErrorMessage } from '@aliasvault/client/api/errors/AppErrorCodes';import { ClientUpgradeRequiredError } from '@aliasvault/client/api/errors/ClientUpgradeRequiredError';import { ServerUpdateRequiredError } from '@aliasvault/client/api/errors/ServerUpdateRequiredError';import { VaultProcessingError } from '@aliasvault/client/api/errors/VaultProcessingError';+import { describeAuthError, formatErrorMessage } from '@aliasvault/client/auth/AuthErrorMessage';import { SrpAuthService } from '@aliasvault/client/auth/SrpAuthService';import { SrpLoginService } from '@aliasvault/client/auth/SrpLoginService';import { VaultKeyService } from '@aliasvault/client/auth/VaultKeyService';@@ -25,7 +24,6 @@ import { useLoading } from '@/entrypoints/popup/context/LoadingContext';import { useWebApi } from '@/entrypoints/popup/context/WebApiContext';import { PopoutUtility } from '@/entrypoints/popup/utils/PopoutUtility';-import { apiErrorMessage } from '@/utils/ApiErrors';import { StorageKeys } from '@/utils/constants/storageKeys';import { logFailure } from '@/utils/Diagnostics';import { sendMessage } from '@/utils/messaging/ExtensionMessaging';@@ -77,10 +75,10 @@ const Login: React.FC = () => {*/const pullAndLoadVault = async (): Promise<void> => {const result = await sendMessage('FULL_VAULT_SYNC', { forcePull: true, reportErrorToPopup: false });- if (result.errorKey === 'clientVersionNotSupported') {+ if (result.logoutReason === 'clientVersionNotSupported') {throw new ClientUpgradeRequiredError();}- if (result.errorKey === 'serverVersionNotSupported') {+ if (result.logoutReason === 'serverVersionNotSupported') {throw new ServerUpdateRequiredError();}if (!result.success) {@@ -95,25 +93,13 @@ const Login: React.FC = () => {* @param context - what failed, for the console* @param err - the error*/- const showLoginError = (context: string, err: unknown): void => {+ const showLoginError = async (context: string, err: unknown): Promise<void> => {logFailure(context, err);- if (err instanceof ClientUpgradeRequiredError) {- // Server refused this client version (HTTP 426).- setError(t('common.errors.clientVersionNotSupported'));- } else if (err instanceof ServerUpdateRequiredError) {- // Server does not support the v2 API, throw unsupported error.- setError(t('common.errors.serverVersionNotSupported'));- } else if (err instanceof VaultProcessingError) {+ if (err instanceof VaultProcessingError) {// The vault was fetched but couldn't be decrypted/materialized, surface the real error (copyable) for support.setVaultError(err);- } else if (apiErrorCodeOf(err)) {- // The server refused the login with a coded reason (wrong password, account locked).- setError(apiErrorMessage(err, t, t('common.errors.serverError')));- } else if (hasErrorCode(err)) {- // Error contains an error code (E-XXX), show the formatted message.- setError(getErrorMessage(err, t('common.errors.serverError')));} else {- setError(t('common.errors.serverError'));+ setError(formatErrorMessage(await describeAuthError(err), t));}};@@ -139,7 +125,13 @@ const Login: React.FC = () => {* Fetch the account's key chain, check the unlock key opens it and cache it as-is; the vault encryption key is* derived from the two on demand. Legacy accounts have no chain.*/- await VaultKeyService.refreshKeyChain(unlockKey, webApi);+ try {+ await VaultKeyService.refreshKeyChain(unlockKey, webApi);+ } catch (err) {+ // If key chain can't be fetched, logout user and show error as this is not a recoverable error.+ await app.logout();+ throw err;+ }await dbContext.storeUnlockKeyDerivationParams({salt: derivationParams.salt,@@ -318,7 +310,7 @@ const Login: React.FC = () => {loginResponse);} catch (err) {- showLoginError('Login error', err);+ await showLoginError('Login error', err);hideLoading();}};@@ -377,7 +369,7 @@ const Login: React.FC = () => {setPasswordHashBase64(null);setLoginResponse(null);} catch (err) {- showLoginError('2FA error', err);+ await showLoginError('2FA error', err);hideLoading();}};@@ -395,12 +387,7 @@ const Login: React.FC = () => {// The mobile device sends the unlock key.await handleSuccessfulAuth(result.username, result.token, result.refreshToken, result.unlockKey, result);} catch (err) {- if (err instanceof ServerUpdateRequiredError) {- // Server does not support the v2 API, throw unsupported error.- setError(t('common.errors.serverVersionNotSupported'));- } else {- setError(err instanceof Error ? err.message : t('common.errors.unknownError'));- }+ await showLoginError('Mobile login error', err);hideLoading();throw err; // Re-throw to let modal show error}diff --git a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsxindex 7e0eacb11..dc42298a6 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx@@ -1,6 +1,6 @@-import { hasErrorCode, getErrorMessage, extractErrorCode } from '@aliasvault/client/api/errors/AppErrorCodes';import { ClientUpgradeRequiredError } from '@aliasvault/client/api/errors/ClientUpgradeRequiredError';import { VaultVersionIncompatibleError } from '@aliasvault/client/api/errors/VaultVersionIncompatibleError';+import { describeAuthError, formatErrorMessage } from '@aliasvault/client/auth/AuthErrorMessage';import { SrpAuthService } from '@aliasvault/client/auth/SrpAuthService';import { SrpLoginService } from '@aliasvault/client/auth/SrpLoginService';import { VaultKeyService } from '@aliasvault/client/auth/VaultKeyService';@@ -120,7 +120,7 @@ const Unlock: React.FC = () => {// Server refused this client version.if (err instanceof ClientUpgradeRequiredError) {setIsInitialLoading(false);- await app.logout(t('common.errors.clientVersionNotSupported'));+ await app.logout(t('common.errors.clientNotSupported'));return { online: false, error: 'clientVersionNotSupported' };}@@ -357,19 +357,17 @@ const Unlock: React.FC = () => {} catch (err) {// Server refused this client version.if (err instanceof ClientUpgradeRequiredError) {- await app.logout(t('common.errors.clientVersionNotSupported'));+ await app.logout(t('common.errors.clientNotSupported'));} else if (err instanceof VaultVersionIncompatibleError) {// Check if it's a version incompatibility errorawait app.logout(err.message);- } else if (hasErrorCode(err)) {- if (await VaultKeyService.isWrongUnlockKey(extractErrorCode(getErrorMessage(err, '')))) {+ } else {+ const message = await describeAuthError(err, { uncodedIsWrongPassword: true });+ if (message.wrongPassword) {await handlePasswordFailedAttempt();} else {- // Other error codes, show the formatted message as-is- setError(getErrorMessage(err, t('common.errors.wrongPassword')));+ setError(formatErrorMessage(message, t));}- } else {- await handlePasswordFailedAttempt();}logFailure('Unlock error', err);} finally {@@ -484,21 +482,17 @@ const Unlock: React.FC = () => {} else if (err instanceof InvalidPinFormatError) {setError(t('settings.unlockMethod.invalidPinFormat'));setPin('');- } else if (hasErrorCode(err)) {- if (await VaultKeyService.isWrongUnlockKey(extractErrorCode(getErrorMessage(err, '')))) {+ } else {+ const message = await describeAuthError(err, { fallback: 'common.errors.unknownErrorTryAgain' });+ if (message.wrongPassword) {// The key the PIN restored does not unlock the vault, treat as incorrect PINlogExpected('[Unlock] The entered PIN did not decrypt the vault', err);setError(t('settings.unlockMethod.incorrectPin', { attemptsRemaining: 3 }));} else {- // Other error codes: show the formatted message as-islogFailure('PIN unlock failed', err);- setError(getErrorMessage(err, t('common.errors.unknownErrorTryAgain')));+ setError(formatErrorMessage(message, t));}setPin('');- } else {- logFailure('PIN unlock failed', err);- setError(t('common.errors.unknownErrorTryAgain'));- setPin('');}hideLoading();}@@ -628,15 +622,12 @@ const Unlock: React.FC = () => {} catch (err) {// Server refused this client version.if (err instanceof ClientUpgradeRequiredError) {- await app.logout(t('common.errors.clientVersionNotSupported'));+ await app.logout(t('common.errors.clientNotSupported'));} else if (err instanceof VaultVersionIncompatibleError) {// Check if it's a version incompatibility errorawait app.logout(err.message);- } else if (hasErrorCode(err)) {- // Error contains an error code (E-XXX), show the formatted message as-is- setError(getErrorMessage(err, t('common.errors.unknownErrorTryAgain')));} else {- setError(t('common.errors.unknownErrorTryAgain'));+ setError(formatErrorMessage(await describeAuthError(err, { fallback: 'common.errors.unknownErrorTryAgain' }), t));}logFailure('Mobile unlock error', err);} finally {diff --git a/apps/browser-extension/src/entrypoints/popup/pages/settings/security/ChangePasswordSettings.tsx b/apps/browser-extension/src/entrypoints/popup/pages/settings/security/ChangePasswordSettings.tsxindex acbce5cb0..bf3a85be1 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/settings/security/ChangePasswordSettings.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/settings/security/ChangePasswordSettings.tsx@@ -1,3 +1,4 @@+import { apiErrorMessage } from '@aliasvault/client/api/errors/ApiErrorMessage';import { IncorrectPasswordError, MasterPasswordService, PasswordChangedElsewhereError } from '@aliasvault/client/auth/MasterPasswordService';import { MIN_ACCEPTED_PASSWORD_LENGTH } from '@aliasvault/client/utilities/PasswordStrength';import React, { useEffect, useState } from 'react';@@ -13,7 +14,6 @@ import { useLoading } from '@/entrypoints/popup/context/LoadingContext';import { useWebApi } from '@/entrypoints/popup/context/WebApiContext';import { useVaultSync } from '@/entrypoints/popup/hooks/useVaultSync';-import { apiErrorMessage } from '@/utils/ApiErrors';import { logFailure } from '@/utils/Diagnostics';import { removeAndDisablePin } from '@/utils/PinUnlockService';diff --git a/apps/browser-extension/src/platform/ExtensionPlatform.ts b/apps/browser-extension/src/platform/ExtensionPlatform.tsindex 0fa03eeca..0af8afdaf 100644--- a/apps/browser-extension/src/platform/ExtensionPlatform.ts+++ b/apps/browser-extension/src/platform/ExtensionPlatform.ts@@ -3,13 +3,15 @@*/import { createRustSqliteEngine } from '@aliasvault/client/database/RustSqliteEngine';-import { type IClientPlatform, type IKeyValueStore, type StorageKey, TranslatableMessage } from '@aliasvault/client/platform';+import { type IClientPlatform, type IKeyValueStore, type StorageKey } from '@aliasvault/client/platform';import { createWasmRustCore } from '@aliasvault/client/rust/WasmRustCore';import { devError, devLog, devWarn } from '@/utils/devLogger/DevLogger';import { t } from '@/i18n/StandaloneI18n';+import type { TranslationKey } from '@aliasvault/i18n';+import { browser, storage } from '#imports';/**@@ -37,15 +39,6 @@ function detectClientName(): 'chrome' | 'firefox' | 'edge' | 'safari' | 'browserreturn 'browser';}-/**- * Which translation key backs each of the core's own messages.- * TODO: refactor this to use centralized translation system instead (on to-do list) once that is implemented.- */-const TRANSLATION_KEYS: Record<TranslatableMessage, string> = {- [TranslatableMessage.ClientOutdated]: 'common.errors.browserExtensionOutdated',- [TranslatableMessage.VaultUpgradeRequired]: 'content.vaultUpgradeRequired',-};-/*** WXT storage.*/@@ -97,7 +90,7 @@ export const extensionPlatform: IClientPlatform = {rustCore,sqlite: createRustSqliteEngine(rustCore),/**- * Translate one of the core's own messages through the extension's i18n.+ * Translate a shared message through the extension's i18n.*/- translate: (message: TranslatableMessage): Promise<string> => t(TRANSLATION_KEYS[message]),+ translate: (key: TranslationKey): Promise<string> => t(key),};diff --git a/apps/browser-extension/src/utils/ApiErrors.ts b/apps/browser-extension/src/utils/ApiErrors.tsdeleted file mode 100644index 542eac341..000000000--- a/apps/browser-extension/src/utils/ApiErrors.ts+++ /dev/null@@ -1,19 +0,0 @@-import { apiErrorCodeOf } from '@aliasvault/client/api/errors/ApiRequestError';--import type { TFunction } from 'i18next';--/**- * The message for a failed API call: the translated API error code when the server sent one, else the fallback.- * @param error - the thrown error- * @param t - the translation function- * @param fallback - the message when no known code is present- */-export function apiErrorMessage(error: unknown, t: TFunction, fallback: string): string {- const code = apiErrorCodeOf(error);- if (code) {- const key = `apiErrors.${code}`;- const translated = t(key);- return translated === key ? fallback : translated;- }- return fallback;-}diff --git a/apps/browser-extension/src/utils/Diagnostics.ts b/apps/browser-extension/src/utils/Diagnostics.tsindex d9321d148..08f76d4d4 100644--- a/apps/browser-extension/src/utils/Diagnostics.ts+++ b/apps/browser-extension/src/utils/Diagnostics.ts@@ -31,7 +31,7 @@ const EXPECTED_ERROR_CODES = new Set<string>([AppErrorCode.UPLOAD_TIMEOUT,AppErrorCode.UPLOAD_TOO_LARGE,AppErrorCode.SERVER_UPDATE_REQUIRED,- AppErrorCode.VERSION_INCOMPATIBLE,+ AppErrorCode.VAULT_VERSION_INCOMPATIBLE,]);/**