From 1257290b11ffd41ca54e9e2035e2c91e7b37075e Mon Sep 17 00:00:00 2001 From: Kristen McWilliam <9575627+Merrit@users.noreply.github.com> Date: Fri, 24 Feb 2023 17:12:41 -0500 Subject: [PATCH] fix(sync_service): don't rethrow error when download fails Download can fail if the file is not found on the server. In this case, we should just return null and let the caller handle the error. --- lib/sync/repository/src/google_drive.dart | 9 ++------- lib/sync/sync_service.dart | 16 +++++----------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/lib/sync/repository/src/google_drive.dart b/lib/sync/repository/src/google_drive.dart index 9e97553..ae8bd1e 100644 --- a/lib/sync/repository/src/google_drive.dart +++ b/lib/sync/repository/src/google_drive.dart @@ -14,10 +14,7 @@ class GoogleDrive implements SyncRepository { @override Future?> download({required String fileName}) async { final fileId = await _getFileId(fileName); - - if (fileId == null) { - throw SyncException('File does not exist.'); - } + if (fileId == null) return null; final Media media; try { @@ -26,9 +23,7 @@ class GoogleDrive implements SyncRepository { downloadOptions: DownloadOptions.fullMedia, ) as Media; } on DetailedApiRequestError catch (e) { - if (e.status == io.HttpStatus.notFound) { - throw SyncException('File does not exist.'); - } + log.e('Failed to download file', e); return null; } diff --git a/lib/sync/sync_service.dart b/lib/sync/sync_service.dart index 97aeb23..73cded1 100644 --- a/lib/sync/sync_service.dart +++ b/lib/sync/sync_service.dart @@ -78,18 +78,12 @@ class SyncService { /// /// Throws a [SyncException] if an error occurs. Future _downloadSyncData() async { - try { - final cloudSyncDataBytes = await _syncRepository.download( - fileName: kSyncDataFileName, - ); + final cloudSyncDataBytes = await _syncRepository.download( + fileName: kSyncDataFileName, + ); - if (cloudSyncDataBytes != null) { - return SyncData.fromBytes(cloudSyncDataBytes); - } - } on SyncException catch (e) { - throw SyncException( - 'Failed to download sync data from cloud service: ${e.message}', - ); + if (cloudSyncDataBytes != null) { + return SyncData.fromBytes(cloudSyncDataBytes); } return null;