Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Update browser extension error reporting

Leendert de Borstf8c24f4647e4
diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
index 2b9f9b71e..eb243a99a 100644
--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
@@ -14,6 +14,7 @@ import { ServiceDetectionUtility } from '@/utils/serviceDetection/ServiceDetecti
import { SqliteClient } from '@/utils/SqliteClient';
import { getItemWithFallback } from '@/utils/StorageUtility';
import { ApiAuthError } from '@/utils/types/errors/ApiAuthError';
+import { ApiRequestError } from '@/utils/types/errors/ApiRequestError';
import { AppErrorCode, formatErrorWithCode } from '@/utils/types/errors/AppErrorCodes';
import { NetworkError } from '@/utils/types/errors/NetworkError';
import { PayloadTooLargeError } from '@/utils/types/errors/PayloadTooLargeError';
@@ -673,6 +674,13 @@ export async function handleUploadVault(
} catch (error) {
console.error('Failed to upload vault:', error);
+ /*
+ * Let network and auth errors propagate.
+ */
+ if (error instanceof NetworkError || error instanceof ApiAuthError) {
+ throw error;
+ }
+
const errorMessage = error instanceof Error ? error.message : '';
// Check if error is UPLOAD_OUTDATED (E-802) - server has newer vault
@@ -689,8 +697,11 @@ export async function handleUploadVault(
return { success: false, error: errorMessage };
}
- // E-801: Upload failed for other reasons
- return { success: false, error: formatErrorWithCode(await t('common.errors.unknownError'), AppErrorCode.UPLOAD_FAILED) };
+ /*
+ * E-801: Upload failed. Include the HTTP status and server error code (if any).
+ */
+ const detail = error instanceof ApiRequestError ? ` [${error.message}]` : '';
+ return { success: false, error: formatErrorWithCode(`${await t('common.errors.unknownError')}${detail}`, AppErrorCode.UPLOAD_FAILED) };
}
}
@@ -828,8 +839,8 @@ async function uploadNewVaultToServer(sqliteClient: SqliteClient) : Promise<Vaul
// Outdated - server has newer version
throw new Error(formatErrorWithCode(await t('common.errors.unknownError'), AppErrorCode.UPLOAD_OUTDATED));
} else {
- // Upload failed
- throw new Error(formatErrorWithCode(await t('common.errors.unknownError'), AppErrorCode.UPLOAD_FAILED));
+ // Upload failed - include the unexpected server status value for diagnosability
+ throw new Error(formatErrorWithCode(`${await t('common.errors.unknownError')} [server status: ${response.status}]`, AppErrorCode.UPLOAD_FAILED));
}
return response;
@@ -1414,8 +1425,8 @@ async function handleFullVaultSyncInternal(): Promise<FullVaultSyncResult> {
return handleFullVaultSync();
} else {
console.error('Server recovery failed:', uploadResponse.error);
- // E-801: Upload failed during server recovery
- return { success: false, hasNewVault: false, wasOffline: false, upgradeRequired: false, requiresLogout: false, error: formatErrorWithCode(await t('common.errors.unknownError'), AppErrorCode.UPLOAD_FAILED) };
+ // E-801: Upload failed during server recovery - preserve the upload error detail when available
+ return { success: false, hasNewVault: false, wasOffline: false, upgradeRequired: false, requiresLogout: false, error: uploadResponse.error ?? formatErrorWithCode(await t('common.errors.unknownError'), AppErrorCode.UPLOAD_FAILED) };
}
}
diff --git a/apps/browser-extension/src/utils/WebApiService.ts b/apps/browser-extension/src/utils/WebApiService.ts
index de6aff160..6856f5493 100644
--- a/apps/browser-extension/src/utils/WebApiService.ts
+++ b/apps/browser-extension/src/utils/WebApiService.ts
@@ -4,6 +4,7 @@ import { logoutEventEmitter } from '@/events/LogoutEventEmitter';
import { AppInfo } from "./AppInfo";
import { ApiAuthError } from './types/errors/ApiAuthError';
+import { ApiRequestError } from './types/errors/ApiRequestError';
import { NetworkError } from './types/errors/NetworkError';
import { PayloadTooLargeError } from './types/errors/PayloadTooLargeError';
@@ -87,7 +88,14 @@ export class WebApiService {
});
if (!retryResponse.ok) {
- throw new ApiAuthError('Request failed after token refresh');
+ // Only auth failures after a successful token refresh mean the session is invalid.
+ if (retryResponse.status === 401 || retryResponse.status === 403) {
+ throw new ApiAuthError('Request failed after token refresh');
+ }
+ if (retryResponse.status === 413) {
+ throw new PayloadTooLargeError(`Request rejected with HTTP 413: payload exceeds server limit`);
+ }
+ throw new ApiRequestError(retryResponse.status, await this.extractApiErrorCode(retryResponse));
}
return parseJson ? retryResponse.json() : retryResponse as unknown as T;
@@ -106,7 +114,7 @@ export class WebApiService {
}
if (!response.ok && throwOnError) {
- throw new Error(`HTTP error! status: ${response.status}`);
+ throw new ApiRequestError(response.status, await this.extractApiErrorCode(response));
}
return parseJson ? response.json() : response as unknown as T;
@@ -116,6 +124,24 @@ export class WebApiService {
}
}
+ /**
+ * Extract the structured API error code (e.g. "VAULT_NOT_UP_TO_DATE") from an error response body.
+ */
+ private async extractApiErrorCode(response: Response): Promise<string | null> {
+ try {
+ const body = await response.clone().json() as { code?: unknown; title?: unknown };
+ for (const value of [body.code, body.title]) {
+ // Server error codes are uppercase enum names
+ if (typeof value === 'string' && /^[A-Z0-9_]{2,64}$/.test(value)) {
+ return value;
+ }
+ }
+ } catch {
+ // Body is empty or not JSON (e.g. proxy error page).
+ }
+ return null;
+ }
+
/**
* Fetch data from the API without authentication headers and without access token refresh retry.
* Throws NetworkError for network-related failures (offline, timeout, DNS, etc.)
diff --git a/apps/browser-extension/src/utils/types/errors/ApiRequestError.ts b/apps/browser-extension/src/utils/types/errors/ApiRequestError.ts
new file mode 100644
index 000000000..53dfaf5fc
--- /dev/null
+++ b/apps/browser-extension/src/utils/types/errors/ApiRequestError.ts
@@ -0,0 +1,24 @@
+/**
+ * Thrown when the server responds to an API request with a non-success HTTP status.
+ */
+export class ApiRequestError extends Error {
+ /** HTTP status code returned by the server. */
+ public readonly statusCode: number;
+
+ /** Structured API error code from the response body, if present. */
+ public readonly apiErrorCode: string | null;
+
+ /**
+ * Creates a new instance of ApiRequestError.
+ *
+ * @param statusCode - The HTTP status code returned by the server.
+ * @param apiErrorCode - The structured API error code from the response body, if present.
+ */
+ public constructor(statusCode: number, apiErrorCode: string | null = null) {
+ super(`HTTP ${statusCode}${apiErrorCode ? `: ${apiErrorCode}` : ''}`);
+ this.name = 'ApiRequestError';
+ this.statusCode = statusCode;
+ this.apiErrorCode = apiErrorCode;
+ Object.setPrototypeOf(this, ApiRequestError.prototype);
+ }
+}