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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 16 deletions

View File

@ -76,12 +76,13 @@ 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) {
@ -226,23 +227,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) {