From adcc618b42a7643b2500c7ed31fbe62a9a2ef3ee Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 23 Nov 2020 18:09:09 +0100 Subject: [PATCH] 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. --- src/services/sync.service.ts | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/services/sync.service.ts b/src/services/sync.service.ts index f60df18c90..055f09a702 100644 --- a/src/services/sync.service.ts +++ b/src/services/sync.service.ts @@ -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) {