TF-1115 Implement catch exception for EmailDataSource

This commit is contained in:
dab246
2022-10-28 12:05:53 +07:00
committed by Dat H. Pham
parent da90a2b2fc
commit d4187eec74
9 changed files with 39 additions and 25 deletions
@@ -78,7 +78,9 @@ class ComposerBindings extends BaseBindings {
Get.lazyPut(() => MailboxCacheDataSourceImpl( Get.lazyPut(() => MailboxCacheDataSourceImpl(
Get.find<MailboxCacheManager>(), Get.find<MailboxCacheManager>(),
Get.find<CacheExceptionThrower>())); Get.find<CacheExceptionThrower>()));
Get.lazyPut(() => EmailDataSourceImpl(Get.find<EmailAPI>())); Get.lazyPut(() => EmailDataSourceImpl(
Get.find<EmailAPI>(),
Get.find<RemoteExceptionThrower>()));
Get.lazyPut(() => HtmlDataSourceImpl( Get.lazyPut(() => HtmlDataSourceImpl(
Get.find<HtmlAnalyzer>(), Get.find<HtmlAnalyzer>(),
Get.find<DioClient>() Get.find<DioClient>()
@@ -71,7 +71,9 @@ class DestinationPickerBindings extends BaseBindings {
Get.find<MailboxCacheManager>(), Get.find<MailboxCacheManager>(),
Get.find<CacheExceptionThrower>())); Get.find<CacheExceptionThrower>()));
Get.lazyPut(() => StateDataSourceImpl(Get.find<StateCacheClient>())); Get.lazyPut(() => StateDataSourceImpl(Get.find<StateCacheClient>()));
Get.lazyPut(() => EmailDataSourceImpl(Get.find<EmailAPI>())); Get.lazyPut(() => EmailDataSourceImpl(
Get.find<EmailAPI>(),
Get.find<RemoteExceptionThrower>()));
Get.lazyPut(() => ThreadDataSourceImpl( Get.lazyPut(() => ThreadDataSourceImpl(
Get.find<ThreadAPI>(), Get.find<ThreadAPI>(),
Get.find<ThreadIsolateWorker>(), Get.find<ThreadIsolateWorker>(),
@@ -1,11 +1,12 @@
import 'dart:async'; import 'dart:async';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:core/core.dart'; import 'package:core/data/network/download/downloaded_response.dart';
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:dartz/dartz.dart'; import 'package:dartz/dartz.dart';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:jmap_dart_client/jmap/account_id.dart'; import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart'; import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:model/model.dart'; import 'package:model/model.dart';
import 'package:tmail_ui_user/features/composer/domain/model/email_request.dart'; import 'package:tmail_ui_user/features/composer/domain/model/email_request.dart';
@@ -13,19 +14,21 @@ import 'package:tmail_ui_user/features/email/data/datasource/email_datasource.da
import 'package:tmail_ui_user/features/email/data/network/email_api.dart'; import 'package:tmail_ui_user/features/email/data/network/email_api.dart';
import 'package:tmail_ui_user/features/email/domain/model/move_to_mailbox_request.dart'; import 'package:tmail_ui_user/features/email/domain/model/move_to_mailbox_request.dart';
import 'package:tmail_ui_user/features/mailbox/domain/model/create_new_mailbox_request.dart'; import 'package:tmail_ui_user/features/mailbox/domain/model/create_new_mailbox_request.dart';
import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart';
class EmailDataSourceImpl extends EmailDataSource { class EmailDataSourceImpl extends EmailDataSource {
final EmailAPI emailAPI; final EmailAPI emailAPI;
final ExceptionThrower _exceptionThrower;
EmailDataSourceImpl(this.emailAPI); EmailDataSourceImpl(this.emailAPI, this._exceptionThrower);
@override @override
Future<Email> getEmailContent(AccountId accountId, EmailId emailId) { Future<Email> getEmailContent(AccountId accountId, EmailId emailId) {
return Future.sync(() async { return Future.sync(() async {
return await emailAPI.getEmailContent(accountId, emailId); return await emailAPI.getEmailContent(accountId, emailId);
}).catchError((error) { }).catchError((error) {
throw error; _exceptionThrower.throwException(error);
}); });
} }
@@ -34,7 +37,7 @@ class EmailDataSourceImpl extends EmailDataSource {
return Future.sync(() async { return Future.sync(() async {
return await emailAPI.sendEmail(accountId, emailRequest, mailboxRequest: mailboxRequest); return await emailAPI.sendEmail(accountId, emailRequest, mailboxRequest: mailboxRequest);
}).catchError((error) { }).catchError((error) {
throw error; _exceptionThrower.throwException(error);
}); });
} }
@@ -43,7 +46,7 @@ class EmailDataSourceImpl extends EmailDataSource {
return Future.sync(() async { return Future.sync(() async {
return await emailAPI.markAsRead(accountId, emails, readActions); return await emailAPI.markAsRead(accountId, emails, readActions);
}).catchError((error) { }).catchError((error) {
throw error; _exceptionThrower.throwException(error);
}); });
} }
@@ -57,7 +60,7 @@ class EmailDataSourceImpl extends EmailDataSource {
return Future.sync(() async { return Future.sync(() async {
return await emailAPI.downloadAttachments(attachments, accountId, baseDownloadUrl, accountRequest); return await emailAPI.downloadAttachments(attachments, accountId, baseDownloadUrl, accountRequest);
}).catchError((error) { }).catchError((error) {
throw error; _exceptionThrower.throwException(error);
}); });
} }
@@ -72,7 +75,7 @@ class EmailDataSourceImpl extends EmailDataSource {
return Future.sync(() async { return Future.sync(() async {
return await emailAPI.exportAttachment(attachment, accountId, baseDownloadUrl, accountRequest, cancelToken); return await emailAPI.exportAttachment(attachment, accountId, baseDownloadUrl, accountRequest, cancelToken);
}).catchError((error) { }).catchError((error) {
throw error; _exceptionThrower.throwException(error);
}); });
} }
@@ -81,7 +84,7 @@ class EmailDataSourceImpl extends EmailDataSource {
return Future.sync(() async { return Future.sync(() async {
return await emailAPI.moveToMailbox(accountId, moveRequest); return await emailAPI.moveToMailbox(accountId, moveRequest);
}).catchError((error) { }).catchError((error) {
throw error; _exceptionThrower.throwException(error);
}); });
} }
@@ -90,7 +93,7 @@ class EmailDataSourceImpl extends EmailDataSource {
return Future.sync(() async { return Future.sync(() async {
return await emailAPI.markAsStar(accountId, emails, markStarAction); return await emailAPI.markAsStar(accountId, emails, markStarAction);
}).catchError((error) { }).catchError((error) {
throw error; _exceptionThrower.throwException(error);
}); });
} }
@@ -99,7 +102,7 @@ class EmailDataSourceImpl extends EmailDataSource {
return Future.sync(() async { return Future.sync(() async {
return await emailAPI.saveEmailAsDrafts(accountId, email); return await emailAPI.saveEmailAsDrafts(accountId, email);
}).catchError((error) { }).catchError((error) {
throw error; _exceptionThrower.throwException(error);
}); });
} }
@@ -108,7 +111,7 @@ class EmailDataSourceImpl extends EmailDataSource {
return Future.sync(() async { return Future.sync(() async {
return await emailAPI.removeEmailDrafts(accountId, emailId); return await emailAPI.removeEmailDrafts(accountId, emailId);
}).catchError((error) { }).catchError((error) {
throw error; _exceptionThrower.throwException(error);
}); });
} }
@@ -117,7 +120,7 @@ class EmailDataSourceImpl extends EmailDataSource {
return Future.sync(() async { return Future.sync(() async {
return await emailAPI.updateEmailDrafts(accountId, newEmail, oldEmailId); return await emailAPI.updateEmailDrafts(accountId, newEmail, oldEmailId);
}).catchError((error) { }).catchError((error) {
throw error; _exceptionThrower.throwException(error);
}); });
} }
@@ -139,7 +142,7 @@ class EmailDataSourceImpl extends EmailDataSource {
accountRequest, accountRequest,
onReceiveController); onReceiveController);
}).catchError((error) { }).catchError((error) {
throw error; _exceptionThrower.throwException(error);
}); });
} }
@@ -148,7 +151,7 @@ class EmailDataSourceImpl extends EmailDataSource {
return Future.sync(() async { return Future.sync(() async {
return await emailAPI.deleteMultipleEmailsPermanently(accountId, emailIds); return await emailAPI.deleteMultipleEmailsPermanently(accountId, emailIds);
}).catchError((error) { }).catchError((error) {
throw error; _exceptionThrower.throwException(error);
}); });
} }
@@ -157,7 +160,7 @@ class EmailDataSourceImpl extends EmailDataSource {
return Future.sync(() async { return Future.sync(() async {
return await emailAPI.deleteEmailPermanently(accountId, emailId); return await emailAPI.deleteEmailPermanently(accountId, emailId);
}).catchError((error) { }).catchError((error) {
throw error; _exceptionThrower.throwException(error);
}); });
} }
} }
@@ -85,7 +85,9 @@ class EmailBindings extends BaseBindings {
Get.lazyPut(() => MailboxCacheDataSourceImpl( Get.lazyPut(() => MailboxCacheDataSourceImpl(
Get.find<MailboxCacheManager>(), Get.find<MailboxCacheManager>(),
Get.find<CacheExceptionThrower>())); Get.find<CacheExceptionThrower>()));
Get.lazyPut(() => EmailDataSourceImpl(Get.find<EmailAPI>())); Get.lazyPut(() => EmailDataSourceImpl(
Get.find<EmailAPI>(),
Get.find<RemoteExceptionThrower>()));
Get.lazyPut(() => HiveAccountDatasourceImpl(Get.find<AccountCacheManager>())); Get.lazyPut(() => HiveAccountDatasourceImpl(Get.find<AccountCacheManager>()));
Get.lazyPut(() => HtmlDataSourceImpl( Get.lazyPut(() => HtmlDataSourceImpl(
Get.find<HtmlAnalyzer>(), Get.find<HtmlAnalyzer>(),
@@ -77,7 +77,9 @@ class MailboxBindings extends BaseBindings {
Get.find<MailboxCacheManager>(), Get.find<MailboxCacheManager>(),
Get.find<CacheExceptionThrower>())); Get.find<CacheExceptionThrower>()));
Get.lazyPut(() => StateDataSourceImpl(Get.find<StateCacheClient>())); Get.lazyPut(() => StateDataSourceImpl(Get.find<StateCacheClient>()));
Get.lazyPut(() => EmailDataSourceImpl(Get.find<EmailAPI>())); Get.lazyPut(() => EmailDataSourceImpl(
Get.find<EmailAPI>(),
Get.find<RemoteExceptionThrower>()));
Get.lazyPut(() => ThreadDataSourceImpl( Get.lazyPut(() => ThreadDataSourceImpl(
Get.find<ThreadAPI>(), Get.find<ThreadAPI>(),
Get.find<ThreadIsolateWorker>(), Get.find<ThreadIsolateWorker>(),
@@ -145,7 +145,9 @@ class MailboxDashBoardBindings extends BaseBindings {
@override @override
void bindingsDataSourceImpl() { void bindingsDataSourceImpl() {
Get.lazyPut(() => EmailDataSourceImpl(Get.find<EmailAPI>())); Get.lazyPut(() => EmailDataSourceImpl(
Get.find<EmailAPI>(),
Get.find<RemoteExceptionThrower>()));
Get.lazyPut(() => HtmlDataSourceImpl( Get.lazyPut(() => HtmlDataSourceImpl(
Get.find<HtmlAnalyzer>(), Get.find<HtmlAnalyzer>(),
Get.find<DioClient>() Get.find<DioClient>()
@@ -92,7 +92,9 @@ class ThreadBindings extends BaseBindings {
Get.find<RemoteExceptionThrower>())); Get.find<RemoteExceptionThrower>()));
Get.lazyPut(() => LocalThreadDataSourceImpl(Get.find<EmailCacheManager>())); Get.lazyPut(() => LocalThreadDataSourceImpl(Get.find<EmailCacheManager>()));
Get.lazyPut(() => StateDataSourceImpl(Get.find<StateCacheClient>())); Get.lazyPut(() => StateDataSourceImpl(Get.find<StateCacheClient>()));
Get.lazyPut(() => EmailDataSourceImpl(Get.find<EmailAPI>())); Get.lazyPut(() => EmailDataSourceImpl(
Get.find<EmailAPI>(),
Get.find<RemoteExceptionThrower>()));
Get.lazyPut(() => HtmlDataSourceImpl( Get.lazyPut(() => HtmlDataSourceImpl(
Get.find<HtmlAnalyzer>(), Get.find<HtmlAnalyzer>(),
Get.find<DioClient>(), Get.find<DioClient>(),
@@ -1,10 +1,9 @@
import 'package:tmail_ui_user/main/exceptions/cache_exception.dart';
import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart'; import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart';
class CacheExceptionThrower extends ExceptionThrower { class CacheExceptionThrower extends ExceptionThrower {
@override @override
void throwException(dynamic error) { void throwException(dynamic error) {
throw UnknownCacheError(message: error.toString()); throw error;
} }
} }
@@ -32,7 +32,7 @@ class RemoteExceptionThrower extends ExceptionThrower {
message: errorResponse.description); message: errorResponse.description);
} }
} else { } else {
throw UnknownError(message: error.toString()); throw error;
} }
} }
} }