Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
sync: move try-catch out of needsSyncing and handle errors it in full…
Browse files Browse the repository at this point in the history
…Sync

The motivation for this is bitwarden/cli#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.
  • Loading branch information
fredrikekre committed Nov 20, 2020
1 parent 9e4d000 commit 8ada0d0
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/services/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,11 @@ export class SyncService implements SyncServiceAbstraction {
}

const now = new Date();
const needsSyncResult = await this.needsSyncing(forceSync);
const needsSync = needsSyncResult[0];
const skipped = needsSyncResult[1];

if (skipped) {
return this.syncCompleted(false);
var needsSync = false;
try {
needsSync = await this.needsSyncing(forceSync);
} catch (e) {
if (allowThrowOnError) { throw e; }
}

if (!needsSync) {
Expand Down Expand Up @@ -226,23 +225,19 @@ export class SyncService implements SyncServiceAbstraction {

private async needsSyncing(forceSync: boolean) {
if (forceSync) {
return [true, false];
return true;
}

const lastSync = await this.getLastSync();
if (lastSync == null || lastSync.getTime() === 0) {
return [true, false];
return true;
}

try {
const response = await this.apiService.getAccountRevisionDate();
if (new Date(response) <= lastSync) {
return [false, false];
}
return [true, false];
} catch (e) {
return [false, true];
const response = await this.apiService.getAccountRevisionDate();
if (new Date(response) <= lastSync) {
return false;
}
return true;
}

private async syncProfile(response: ProfileResponse) {
Expand Down

0 comments on commit 8ada0d0

Please sign in to comment.