1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 16:23:44 +00:00

sync: move try-catch out of needsSyncing and handle errors it in fullSync (#207)

The motivation for this is https://github.com/bitwarden/cli/issues/129
where failed sync's are swallowed by try-catch. By moving the try-catch
to the outside it is possible to reuse the already existing
allowThrowOnError argument which callers can use to signal whether
fullSync should throw or ignore errors silently. This patch is
companioned with a patch to the SyncCommand CLI command to pass
allowThrowOnError.
This commit is contained in:
Fredrik Ekre
2020-11-23 18:09:09 +01:00
committed by GitHub
parent cd6b3d47c2
commit adcc618b42

View File

@@ -76,12 +76,13 @@ export class SyncService implements SyncServiceAbstraction {
} }
const now = new Date(); const now = new Date();
const needsSyncResult = await this.needsSyncing(forceSync); var needsSync = false;
const needsSync = needsSyncResult[0]; try {
const skipped = needsSyncResult[1]; needsSync = await this.needsSyncing(forceSync);
} catch (e) {
if (skipped) { if (allowThrowOnError) {
return this.syncCompleted(false); throw e;
}
} }
if (!needsSync) { if (!needsSync) {
@@ -226,23 +227,19 @@ export class SyncService implements SyncServiceAbstraction {
private async needsSyncing(forceSync: boolean) { private async needsSyncing(forceSync: boolean) {
if (forceSync) { if (forceSync) {
return [true, false]; return true;
} }
const lastSync = await this.getLastSync(); const lastSync = await this.getLastSync();
if (lastSync == null || lastSync.getTime() === 0) { if (lastSync == null || lastSync.getTime() === 0) {
return [true, false]; return true;
} }
try {
const response = await this.apiService.getAccountRevisionDate(); const response = await this.apiService.getAccountRevisionDate();
if (new Date(response) <= lastSync) { if (new Date(response) <= lastSync) {
return [false, false]; return false;
}
return [true, false];
} catch (e) {
return [false, true];
} }
return true;
} }
private async syncProfile(response: ProfileResponse) { private async syncProfile(response: ProfileResponse) {