Skip to content

Commit

Permalink
fix(sync_service): don't rethrow error when download fails
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Merrit committed Feb 24, 2023
1 parent be7b762 commit 1257290
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
9 changes: 2 additions & 7 deletions lib/sync/repository/src/google_drive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ class GoogleDrive implements SyncRepository {
@override
Future<List<int>?> 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 {
Expand All @@ -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;
}

Expand Down
16 changes: 5 additions & 11 deletions lib/sync/sync_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,12 @@ class SyncService {
///
/// Throws a [SyncException] if an error occurs.
Future<SyncData?> _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;
Expand Down

0 comments on commit 1257290

Please sign in to comment.