From fb177fb33ddc7d066656a6860ff7ca654abc66aa Mon Sep 17 00:00:00 2001 From: dab246 Date: Thu, 27 Oct 2022 16:59:31 +0700 Subject: [PATCH] TF-1115 Add `RemoteExceptionThrower` --- core/lib/core.dart | 2 - .../network/remote_exception_thrower.dart | 24 ----------- .../domain/exceptions/remote_exception.dart | 25 ----------- .../destination_picker_bindings.dart | 6 ++- .../exceptions/authentication_exception.dart | 2 +- .../presentation/mailbox_bindings.dart | 6 ++- .../bindings/mailbox_dashboard_bindings.dart | 6 ++- .../session_datasource_impl.dart | 21 ++------- .../presentation/session_controller.dart | 2 +- .../bindings/network/network_bindings.dart | 1 + .../bindings/session/session_bindings.dart | 2 +- lib/main/exceptions/exception_thrower.dart | 4 ++ lib/main/exceptions/remote_exception.dart | 43 +++++++++++++++++++ .../exceptions/remote_exception_thrower.dart | 38 ++++++++++++++++ 14 files changed, 108 insertions(+), 74 deletions(-) delete mode 100644 core/lib/data/network/remote_exception_thrower.dart delete mode 100644 core/lib/domain/exceptions/remote_exception.dart create mode 100644 lib/main/exceptions/exception_thrower.dart create mode 100644 lib/main/exceptions/remote_exception.dart create mode 100644 lib/main/exceptions/remote_exception_thrower.dart diff --git a/core/lib/core.dart b/core/lib/core.dart index 6247f20f5..5cc4a0374 100644 --- a/core/lib/core.dart +++ b/core/lib/core.dart @@ -15,8 +15,6 @@ export 'domain/extensions/media_type_extension.dart'; // Exceptions export 'domain/exceptions/download_file_exception.dart'; export 'data/extensions/options_extensions.dart'; -export 'domain/exceptions/remote_exception.dart'; -export 'data/network/remote_exception_thrower.dart'; export 'domain/exceptions/web_session_exception.dart'; // Utils diff --git a/core/lib/data/network/remote_exception_thrower.dart b/core/lib/data/network/remote_exception_thrower.dart deleted file mode 100644 index 3074d9980..000000000 --- a/core/lib/data/network/remote_exception_thrower.dart +++ /dev/null @@ -1,24 +0,0 @@ - -import 'package:core/domain/exceptions/remote_exception.dart'; -import 'package:dio/dio.dart'; - -class RemoteExceptionThrower { - void throwRemoteException(dynamic exception, {Function(DioError)? handler}) { - if (exception is DioError) { - switch (exception.type) { - case DioErrorType.connectTimeout: - throw ConnectError(); - default: - if (handler != null) { - throw handler(exception); - } else { - throw UnknownError( - code: exception.response?.statusCode, - message: exception.message); - } - } - } else { - throw UnknownError(message: exception.toString()); - } - } -} diff --git a/core/lib/domain/exceptions/remote_exception.dart b/core/lib/domain/exceptions/remote_exception.dart deleted file mode 100644 index 0d1c4a631..000000000 --- a/core/lib/domain/exceptions/remote_exception.dart +++ /dev/null @@ -1,25 +0,0 @@ - -import 'package:equatable/equatable.dart'; - -abstract class RemoteException extends Equatable implements Exception { - static const connectError = 'Connect error'; - - final String? message; - final int? code; - - const RemoteException({this.code, this.message}); -} - -class UnknownError extends RemoteException { - const UnknownError({int? code, String? message}) : super(code: code, message: message); - - @override - List get props => []; -} - -class ConnectError extends RemoteException { - const ConnectError() : super(message: RemoteException.connectError); - - @override - List get props => []; -} \ No newline at end of file diff --git a/lib/features/destination_picker/presentation/destination_picker_bindings.dart b/lib/features/destination_picker/presentation/destination_picker_bindings.dart index 8f64400c6..2268fff60 100644 --- a/lib/features/destination_picker/presentation/destination_picker_bindings.dart +++ b/lib/features/destination_picker/presentation/destination_picker_bindings.dart @@ -26,6 +26,7 @@ import 'package:tmail_ui_user/features/thread/data/datasource/thread_datasource. import 'package:tmail_ui_user/features/thread/data/datasource_impl/thread_datasource_impl.dart'; import 'package:tmail_ui_user/features/thread/data/network/thread_api.dart'; import 'package:tmail_ui_user/features/thread/data/network/thread_isolate_worker.dart'; +import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart'; class DestinationPickerBindings extends BaseBindings { @@ -65,7 +66,10 @@ class DestinationPickerBindings extends BaseBindings { Get.lazyPut(() => MailboxCacheDataSourceImpl(Get.find())); Get.lazyPut(() => StateDataSourceImpl(Get.find())); Get.lazyPut(() => EmailDataSourceImpl(Get.find())); - Get.lazyPut(() => ThreadDataSourceImpl(Get.find(), Get.find())); + Get.lazyPut(() => ThreadDataSourceImpl( + Get.find(), + Get.find(), + Get.find())); } @override diff --git a/lib/features/login/domain/exceptions/authentication_exception.dart b/lib/features/login/domain/exceptions/authentication_exception.dart index c3c960830..87ff5b2c7 100644 --- a/lib/features/login/domain/exceptions/authentication_exception.dart +++ b/lib/features/login/domain/exceptions/authentication_exception.dart @@ -1,5 +1,5 @@ -import 'package:core/domain/exceptions/remote_exception.dart'; +import 'package:tmail_ui_user/main/exceptions/remote_exception.dart'; abstract class AuthenticationException extends RemoteException { static const wrongCredential = 'Credential is wrong'; diff --git a/lib/features/mailbox/presentation/mailbox_bindings.dart b/lib/features/mailbox/presentation/mailbox_bindings.dart index 81ecaef6c..1919074e7 100644 --- a/lib/features/mailbox/presentation/mailbox_bindings.dart +++ b/lib/features/mailbox/presentation/mailbox_bindings.dart @@ -29,6 +29,7 @@ import 'package:tmail_ui_user/features/thread/data/datasource/thread_datasource. import 'package:tmail_ui_user/features/thread/data/datasource_impl/thread_datasource_impl.dart'; import 'package:tmail_ui_user/features/thread/data/network/thread_api.dart'; import 'package:tmail_ui_user/features/thread/data/network/thread_isolate_worker.dart'; +import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart'; class MailboxBindings extends BaseBindings { @@ -71,7 +72,10 @@ class MailboxBindings extends BaseBindings { Get.lazyPut(() => MailboxCacheDataSourceImpl(Get.find())); Get.lazyPut(() => StateDataSourceImpl(Get.find())); Get.lazyPut(() => EmailDataSourceImpl(Get.find())); - Get.lazyPut(() => ThreadDataSourceImpl(Get.find(), Get.find())); + Get.lazyPut(() => ThreadDataSourceImpl( + Get.find(), + Get.find(), + Get.find())); } @override diff --git a/lib/features/mailbox_dashboard/presentation/bindings/mailbox_dashboard_bindings.dart b/lib/features/mailbox_dashboard/presentation/bindings/mailbox_dashboard_bindings.dart index 1c0931d9e..891113c3e 100644 --- a/lib/features/mailbox_dashboard/presentation/bindings/mailbox_dashboard_bindings.dart +++ b/lib/features/mailbox_dashboard/presentation/bindings/mailbox_dashboard_bindings.dart @@ -88,6 +88,7 @@ import 'package:tmail_ui_user/features/thread/domain/usecases/move_multiple_emai import 'package:tmail_ui_user/features/thread/domain/usecases/search_email_interactor.dart'; import 'package:tmail_ui_user/features/thread/domain/usecases/search_more_email_interactor.dart'; import 'package:tmail_ui_user/features/thread/presentation/thread_bindings.dart'; +import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart'; class MailboxDashBoardBindings extends BaseBindings { @@ -149,7 +150,10 @@ class MailboxDashBoardBindings extends BaseBindings { Get.find() )); Get.lazyPut(() => SearchDataSourceImpl(Get.find())); - Get.lazyPut(() => ThreadDataSourceImpl(Get.find(), Get.find())); + Get.lazyPut(() => ThreadDataSourceImpl( + Get.find(), + Get.find(), + Get.find())); Get.lazyPut(() => LocalThreadDataSourceImpl(Get.find())); Get.lazyPut(() => StateDataSourceImpl(Get.find())); Get.lazyPut(() => MailboxDataSourceImpl(Get.find(), Get.find())); diff --git a/lib/features/session/data/datasource_impl/session_datasource_impl.dart b/lib/features/session/data/datasource_impl/session_datasource_impl.dart index f0ea39031..5b68cfc07 100644 --- a/lib/features/session/data/datasource_impl/session_datasource_impl.dart +++ b/lib/features/session/data/datasource_impl/session_datasource_impl.dart @@ -1,34 +1,21 @@ -import 'package:core/data/network/remote_exception_thrower.dart'; -import 'package:core/domain/exceptions/remote_exception.dart'; -import 'package:core/utils/app_logger.dart'; -import 'package:dio/dio.dart'; import 'package:jmap_dart_client/jmap/core/session/session.dart'; -import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart'; import 'package:tmail_ui_user/features/session/data/datasource/session_datasource.dart'; import 'package:tmail_ui_user/features/session/data/network/session_api.dart'; +import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart'; class SessionDataSourceImpl extends SessionDataSource { final SessionAPI _sessionAPI; - final RemoteExceptionThrower _remoteExceptionThrower; + final ExceptionThrower _exceptionThrower; - SessionDataSourceImpl(this._sessionAPI, this._remoteExceptionThrower); + SessionDataSourceImpl(this._sessionAPI, this._exceptionThrower); @override Future getSession() { return Future.sync(() async { return await _sessionAPI.getSession(); }).catchError((error) { - _remoteExceptionThrower.throwRemoteException(error, handler: (DioError error) { - if (error.response?.statusCode == 502) { - throw BadGateway(); - } else { - log('SessionDataSourceImpl::getSession(): statusMessage: ${error.response?.statusMessage}'); - throw UnknownError( - code: error.response?.statusCode, - message: error.response?.statusMessage); - } - }); + _exceptionThrower.throwException(error); }); } } \ No newline at end of file diff --git a/lib/features/session/presentation/session_controller.dart b/lib/features/session/presentation/session_controller.dart index b8a5da9e4..9d9f69518 100644 --- a/lib/features/session/presentation/session_controller.dart +++ b/lib/features/session/presentation/session_controller.dart @@ -1,7 +1,6 @@ import 'package:core/data/network/config/dynamic_url_interceptors.dart'; import 'package:core/presentation/state/failure.dart'; import 'package:core/presentation/utils/app_toast.dart'; -import 'package:core/domain/exceptions/remote_exception.dart'; import 'package:get/get.dart'; import 'package:jmap_dart_client/jmap/core/session/session.dart'; import 'package:tmail_ui_user/features/base/reloadable/reloadable_controller.dart'; @@ -14,6 +13,7 @@ import 'package:tmail_ui_user/features/login/domain/usecases/get_authenticated_a import 'package:tmail_ui_user/features/manage_account/domain/usecases/log_out_oidc_interactor.dart'; import 'package:tmail_ui_user/features/session/domain/state/get_session_state.dart'; import 'package:tmail_ui_user/features/session/domain/usecases/get_session_interactor.dart'; +import 'package:tmail_ui_user/main/exceptions/remote_exception.dart'; import 'package:tmail_ui_user/main/localizations/app_localizations.dart'; import 'package:tmail_ui_user/main/routes/app_routes.dart'; import 'package:tmail_ui_user/main/routes/route_navigation.dart'; diff --git a/lib/main/bindings/network/network_bindings.dart b/lib/main/bindings/network/network_bindings.dart index 88f11c9e9..3f727a8a8 100644 --- a/lib/main/bindings/network/network_bindings.dart +++ b/lib/main/bindings/network/network_bindings.dart @@ -22,6 +22,7 @@ import 'package:tmail_ui_user/features/manage_account/data/network/manage_accoun import 'package:tmail_ui_user/features/manage_account/data/network/vacation_api.dart'; import 'package:tmail_ui_user/features/session/data/network/session_api.dart'; import 'package:tmail_ui_user/features/thread/data/network/thread_api.dart'; +import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart'; class NetworkBindings extends Bindings { diff --git a/lib/main/bindings/session/session_bindings.dart b/lib/main/bindings/session/session_bindings.dart index daa9db5b6..801f1f44e 100644 --- a/lib/main/bindings/session/session_bindings.dart +++ b/lib/main/bindings/session/session_bindings.dart @@ -1,4 +1,3 @@ -import 'package:core/core.dart'; import 'package:get/get.dart'; import 'package:tmail_ui_user/features/base/base_bindings.dart'; import 'package:tmail_ui_user/features/session/data/datasource/session_datasource.dart'; @@ -7,6 +6,7 @@ import 'package:tmail_ui_user/features/session/data/network/session_api.dart'; import 'package:tmail_ui_user/features/session/data/repository/session_repository_impl.dart'; import 'package:tmail_ui_user/features/session/domain/repository/session_repository.dart'; import 'package:tmail_ui_user/features/session/domain/usecases/get_session_interactor.dart'; +import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart'; class SessionBindings extends BaseBindings { diff --git a/lib/main/exceptions/exception_thrower.dart b/lib/main/exceptions/exception_thrower.dart new file mode 100644 index 000000000..2a1294686 --- /dev/null +++ b/lib/main/exceptions/exception_thrower.dart @@ -0,0 +1,4 @@ + +abstract class ExceptionThrower { + void throwException(dynamic exception); +} diff --git a/lib/main/exceptions/remote_exception.dart b/lib/main/exceptions/remote_exception.dart new file mode 100644 index 000000000..35f305d1e --- /dev/null +++ b/lib/main/exceptions/remote_exception.dart @@ -0,0 +1,43 @@ + +import 'package:equatable/equatable.dart'; +import 'package:jmap_dart_client/jmap/core/error/error_type.dart'; +import 'package:jmap_dart_client/jmap/core/error/method/error_method_response.dart'; + +abstract class RemoteException with EquatableMixin implements Exception { + static const connectError = 'Connect error'; + + final String? message; + final int? code; + + const RemoteException({this.code, this.message}); +} + +class UnknownError extends RemoteException { + const UnknownError({int? code, String? message}) : super(code: code, message: message); + + @override + List get props => []; +} + +class ConnectError extends RemoteException { + const ConnectError() : super(message: RemoteException.connectError); + + @override + List get props => []; +} + +class MethodLevelErrors extends RemoteException { + final ErrorType type; + + const MethodLevelErrors( + this.type, + {String? message} + ) : super(message: message); + + @override + List get props => [type, message]; +} + +class CannotCalculateChangesMethodResponseException extends MethodLevelErrors { + CannotCalculateChangesMethodResponseException({String? message}) : super(ErrorMethodResponse.cannotCalculateChanges, message: message); +} \ No newline at end of file diff --git a/lib/main/exceptions/remote_exception_thrower.dart b/lib/main/exceptions/remote_exception_thrower.dart new file mode 100644 index 000000000..50903378d --- /dev/null +++ b/lib/main/exceptions/remote_exception_thrower.dart @@ -0,0 +1,38 @@ +import 'package:dio/dio.dart'; +import 'package:jmap_dart_client/jmap/core/error/method/error_method_response.dart'; +import 'package:jmap_dart_client/jmap/core/error/method/exception/error_method_response_exception.dart'; +import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart'; +import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart'; +import 'package:tmail_ui_user/main/exceptions/remote_exception.dart'; + +class RemoteExceptionThrower extends ExceptionThrower { + + @override + void throwException(dynamic exception) { + if (exception is DioError) { + switch (exception.type) { + case DioErrorType.connectTimeout: + throw const ConnectError(); + default: + if (exception.response?.statusCode == 502) { + throw BadGateway(); + } else { + throw UnknownError( + code: exception.response?.statusCode, + message: exception.response?.statusMessage); + } + } + } else if (exception is ErrorMethodResponseException) { + final errorResponse = exception.errorResponse as ErrorMethodResponse; + if (errorResponse is CannotCalculateChangesMethodResponse) { + throw CannotCalculateChangesMethodResponseException(); + } else { + throw MethodLevelErrors( + errorResponse.type, + message: errorResponse.description); + } + } else { + throw UnknownError(message: exception.toString()); + } + } +} \ No newline at end of file