TF-1963 Sync handle exception at data and domain layer

(cherry picked from commit 591388e01fc4fc4c357bdb40a44dac0377dc9fe5)
This commit is contained in:
dab246
2023-06-26 21:00:30 +07:00
committed by Dat Vu
parent badb7509b6
commit 8be3eb53e9
20 changed files with 77 additions and 64 deletions
@@ -37,5 +37,5 @@ abstract class AuthenticationOIDCDataSource {
String discoveryUrl, String discoveryUrl,
List<String> scopes); List<String> scopes);
Future<String?> getAuthenticationInfo(); Future<String> getAuthenticationInfo();
} }
@@ -25,8 +25,7 @@ class AuthenticationOIDCDataSourceImpl extends AuthenticationOIDCDataSource {
@override @override
Future<OIDCResponse> checkOIDCIsAvailable(OIDCRequest oidcRequest) { Future<OIDCResponse> checkOIDCIsAvailable(OIDCRequest oidcRequest) {
return Future.sync(() async { return Future.sync(() async {
final oidcResponse = await _oidcHttpClient.checkOIDCIsAvailable(oidcRequest); return await _oidcHttpClient.checkOIDCIsAvailable(oidcRequest);
return oidcResponse!;
}).catchError(_exceptionThrower.throwException); }).catchError(_exceptionThrower.throwException);
} }
@@ -128,7 +127,7 @@ class AuthenticationOIDCDataSourceImpl extends AuthenticationOIDCDataSource {
} }
@override @override
Future<String?> getAuthenticationInfo() { Future<String> getAuthenticationInfo() {
return Future.sync(() async { return Future.sync(() async {
return await _authenticationClient.getAuthenticationInfo(); return await _authenticationClient.getAuthenticationInfo();
}).catchError(_exceptionThrower.throwException); }).catchError(_exceptionThrower.throwException);
@@ -13,7 +13,7 @@ abstract class AuthenticationClientBase {
String discoveryUrl, String discoveryUrl,
List<String> scopes); List<String> scopes);
Future<String?> getAuthenticationInfo(); Future<String> getAuthenticationInfo();
Future<TokenOIDC> getTokenOIDC( Future<TokenOIDC> getTokenOIDC(
String clientId, String clientId,
@@ -93,8 +93,8 @@ class AuthenticationClientMobile implements AuthenticationClientBase {
} }
@override @override
Future<String?> getAuthenticationInfo() { Future<String> getAuthenticationInfo() {
return Future.value(null); return Future.value('');
} }
} }
@@ -97,10 +97,14 @@ class AuthenticationClientWeb implements AuthenticationClientBase {
} }
@override @override
Future<String?> getAuthenticationInfo() async { Future<String> getAuthenticationInfo() async {
final authUrl = html.window.sessionStorage[OIDCConstant.authResponseKey]; final authUrl = html.window.sessionStorage[OIDCConstant.authResponseKey];
log('AuthenticationClientWeb::getAuthenticationInfo(): authUrl: $authUrl'); log('AuthenticationClientWeb::getAuthenticationInfo(): authUrl: $authUrl');
return authUrl; if (authUrl != null && authUrl.isNotEmpty) {
return authUrl;
} else {
throw CanNotAuthenticationInfoOnWeb();
}
} }
} }
@@ -1 +1,5 @@
class CanNotFoundOIDCAuthority implements Exception {} class CanNotFoundOIDCAuthority implements Exception {}
class CanNotFoundOIDCLinks implements Exception {}
class CanNotFoundToken implements Exception {}
@@ -20,7 +20,7 @@ class OIDCHttpClient {
OIDCHttpClient(this._dioClient); OIDCHttpClient(this._dioClient);
Future<OIDCResponse?> checkOIDCIsAvailable(OIDCRequest oidcRequest) async { Future<OIDCResponse> checkOIDCIsAvailable(OIDCRequest oidcRequest) async {
final result = await _dioClient.get( final result = await _dioClient.get(
Endpoint.webFinger Endpoint.webFinger
.generateOIDCPath(Uri.parse(oidcRequest.baseUrl)) .generateOIDCPath(Uri.parse(oidcRequest.baseUrl))
@@ -31,10 +31,14 @@ class OIDCHttpClient {
.generateEndpointPath() .generateEndpointPath()
); );
log('OIDCHttpClient::checkOIDCIsAvailable(): RESULT: $result'); log('OIDCHttpClient::checkOIDCIsAvailable(): RESULT: $result');
if (result is Map<String, dynamic>) { if (result != null) {
return OIDCResponse.fromJson(result); if (result is Map<String, dynamic>) {
return OIDCResponse.fromJson(result);
} else {
return OIDCResponse.fromJson(jsonDecode(result));
}
} else { } else {
return OIDCResponse.fromJson(jsonDecode(result)); throw CanNotFoundOIDCLinks();
} }
} }
@@ -86,7 +86,7 @@ class AuthenticationOIDCRepositoryImpl extends AuthenticationOIDCRepository {
} }
@override @override
Future<String?> getAuthenticationInfo() { Future<String> getAuthenticationInfo() {
return _oidcDataSource.getAuthenticationInfo(); return _oidcDataSource.getAuthenticationInfo();
} }
@@ -23,13 +23,9 @@ class BadGateway extends AuthenticationException {
List<Object?> get props => [message]; List<Object?> get props => [message];
} }
class NotFoundAuthenticatedAccountException implements Exception { class NotFoundAuthenticatedAccountException implements Exception {}
NotFoundAuthenticatedAccountException();
}
class NotFoundStoredTokenException implements Exception { class NotFoundStoredTokenException implements Exception {}
NotFoundStoredTokenException();
}
class InvalidBaseUrl extends AuthenticationException { class InvalidBaseUrl extends AuthenticationException {
InvalidBaseUrl() : super(AuthenticationException.invalidBaseUrl); InvalidBaseUrl() : super(AuthenticationException.invalidBaseUrl);
@@ -38,13 +34,9 @@ class InvalidBaseUrl extends AuthenticationException {
List<Object?> get props => [message]; List<Object?> get props => [message];
} }
class NotFoundAccessTokenException implements Exception { class NotFoundAccessTokenException implements Exception {}
NotFoundAccessTokenException();
}
class AccessTokenInvalidException implements Exception { class AccessTokenInvalidException implements Exception {}
AccessTokenInvalidException();
}
class DownloadAttachmentHasTokenExpiredException implements Exception { class DownloadAttachmentHasTokenExpiredException implements Exception {
@@ -52,3 +44,11 @@ class DownloadAttachmentHasTokenExpiredException implements Exception {
DownloadAttachmentHasTokenExpiredException(this.refreshToken); DownloadAttachmentHasTokenExpiredException(this.refreshToken);
} }
class CanNotFoundBaseUrl implements Exception {}
class CanNotFoundUserName implements Exception {}
class CanNotFoundPassword implements Exception {}
class CanNotAuthenticationInfoOnWeb implements Exception {}
@@ -37,5 +37,5 @@ abstract class AuthenticationOIDCRepository {
String discoveryUrl, String discoveryUrl,
List<String> scopes); List<String> scopes);
Future<String?> getAuthenticationInfo(); Future<String> getAuthenticationInfo();
} }
@@ -18,9 +18,8 @@ class AuthenticationUserSuccess extends UIState {
} }
class AuthenticationUserFailure extends FeatureFailure { class AuthenticationUserFailure extends FeatureFailure {
final dynamic exception;
AuthenticationUserFailure(this.exception); AuthenticationUserFailure(dynamic exception) : super(exception: exception);
@override @override
List<Object?> get props => [exception]; List<Object?> get props => [exception];
@@ -20,9 +20,8 @@ class CheckOIDCIsAvailableSuccess extends UIState {
} }
class CheckOIDCIsAvailableFailure extends FeatureFailure { class CheckOIDCIsAvailableFailure extends FeatureFailure {
final dynamic exception;
CheckOIDCIsAvailableFailure(this.exception); CheckOIDCIsAvailableFailure(dynamic exception) : super(exception: exception);
@override @override
List<Object?> get props => [exception]; List<Object?> get props => [exception];
@@ -19,9 +19,8 @@ class GetAuthenticationInfoSuccess extends UIState {
} }
class GetAuthenticationInfoFailure extends FeatureFailure { class GetAuthenticationInfoFailure extends FeatureFailure {
final dynamic exception;
GetAuthenticationInfoFailure(this.exception); GetAuthenticationInfoFailure(dynamic exception) : super(exception: exception);
@override @override
List<Object?> get props => [exception]; List<Object?> get props => [exception];
@@ -20,9 +20,8 @@ class GetOIDCConfigurationSuccess extends UIState {
} }
class GetOIDCConfigurationFailure extends FeatureFailure { class GetOIDCConfigurationFailure extends FeatureFailure {
final dynamic exception;
GetOIDCConfigurationFailure(this.exception); GetOIDCConfigurationFailure(dynamic exception) : super(exception: exception);
@override @override
List<Object?> get props => [exception]; List<Object?> get props => [exception];
@@ -20,9 +20,8 @@ class GetOIDCIsAvailableSuccess extends UIState {
} }
class GetOIDCIsAvailableFailure extends FeatureFailure { class GetOIDCIsAvailableFailure extends FeatureFailure {
final dynamic exception;
GetOIDCIsAvailableFailure(this.exception); GetOIDCIsAvailableFailure(dynamic exception) : super(exception: exception);
@override @override
List<Object?> get props => [exception]; List<Object?> get props => [exception];
@@ -21,9 +21,8 @@ class GetTokenOIDCSuccess extends UIState {
} }
class GetTokenOIDCFailure extends FeatureFailure { class GetTokenOIDCFailure extends FeatureFailure {
final dynamic exception;
GetTokenOIDCFailure(this.exception); GetTokenOIDCFailure(dynamic exception) : super(exception: exception);
@override @override
List<Object?> get props => [exception]; List<Object?> get props => [exception];
@@ -7,6 +7,7 @@ import 'package:model/account/authentication_type.dart';
import 'package:model/account/password.dart'; import 'package:model/account/password.dart';
import 'package:model/account/personal_account.dart'; import 'package:model/account/personal_account.dart';
import 'package:tmail_ui_user/features/login/data/model/authentication_info_cache.dart'; import 'package:tmail_ui_user/features/login/data/model/authentication_info_cache.dart';
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
import 'package:tmail_ui_user/features/login/domain/repository/account_repository.dart'; import 'package:tmail_ui_user/features/login/domain/repository/account_repository.dart';
import 'package:tmail_ui_user/features/login/domain/repository/authentication_repository.dart'; import 'package:tmail_ui_user/features/login/domain/repository/authentication_repository.dart';
import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart'; import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart';
@@ -23,20 +24,31 @@ class AuthenticationInteractor {
this._accountRepository this._accountRepository
); );
Stream<Either<Failure, Success>> execute(Uri baseUrl, UserName userName, Password password) async* { Stream<Either<Failure, Success>> execute({Uri? baseUrl, UserName? userName, Password? password}) async* {
try { try {
yield Right(AuthenticationUserLoading()); yield Right(AuthenticationUserLoading());
final user = await authenticationRepository.authenticationUser(baseUrl, userName, password);
await Future.wait([ if (baseUrl != null && userName != null && password != null) {
credentialRepository.saveBaseUrl(baseUrl), final user = await authenticationRepository.authenticationUser(baseUrl, userName, password);
credentialRepository.storeAuthenticationInfo(AuthenticationInfoCache(userName.value, password.value)), await Future.wait([
_accountRepository.setCurrentAccount(PersonalAccount( credentialRepository.saveBaseUrl(baseUrl),
userName.value, credentialRepository.storeAuthenticationInfo(AuthenticationInfoCache(userName.value, password.value)),
AuthenticationType.basic, _accountRepository.setCurrentAccount(PersonalAccount(
isSelected: true userName.value,
)) AuthenticationType.basic,
]); isSelected: true
yield Right(AuthenticationUserSuccess(user)); ))
]);
yield Right(AuthenticationUserSuccess(user));
} else if (baseUrl == null) {
yield Left(AuthenticationUserFailure(CanNotFoundBaseUrl()));
} else if (userName == null) {
yield Left(AuthenticationUserFailure(CanNotFoundUserName()));
} else if (password == null) {
yield Left(AuthenticationUserFailure(CanNotFoundPassword()));
} else {
yield Left(AuthenticationUserFailure(null));
}
} catch (e) { } catch (e) {
logError('AuthenticationInteractor::execute(): $e'); logError('AuthenticationInteractor::execute(): $e');
yield Left(AuthenticationUserFailure(e)); yield Left(AuthenticationUserFailure(e));
@@ -15,11 +15,7 @@ class GetAuthenticationInfoInteractor {
yield Right<Failure, Success>(GetAuthenticationInfoLoading()); yield Right<Failure, Success>(GetAuthenticationInfoLoading());
final result = await _oidcRepository.getAuthenticationInfo(); final result = await _oidcRepository.getAuthenticationInfo();
log('GetAuthenticationInfoInteractor::execute(): result: $result'); log('GetAuthenticationInfoInteractor::execute(): result: $result');
if (result?.isNotEmpty == true) { yield Right<Failure, Success>(GetAuthenticationInfoSuccess());
yield Right<Failure, Success>(GetAuthenticationInfoSuccess());
} else {
yield Left<Failure, Success>(GetAuthenticationInfoFailure(null));
}
} catch (e) { } catch (e) {
log('GetAuthenticationInfoInteractor::execute(): ERROR: $e'); log('GetAuthenticationInfoInteractor::execute(): ERROR: $e');
yield Left<Failure, Success>(GetAuthenticationInfoFailure(e)); yield Left<Failure, Success>(GetAuthenticationInfoFailure(e));
@@ -10,12 +10,12 @@ class SaveLoginUrlOnMobileInteractor {
SaveLoginUrlOnMobileInteractor(this.loginUrlRepository); SaveLoginUrlOnMobileInteractor(this.loginUrlRepository);
Stream<Either<Failure, Success>> execute(RecentLoginUrl recentLoginUrl) async* { Future<Either<Failure, Success>> execute(RecentLoginUrl recentLoginUrl) async {
try{ try{
await loginUrlRepository.saveRecentLoginUrl(recentLoginUrl); await loginUrlRepository.saveRecentLoginUrl(recentLoginUrl);
yield Right(SaveRecentLoginUrlSuccess()); return Right(SaveRecentLoginUrlSuccess());
} catch(e) { } catch(e) {
yield Left(SaveRecentLoginUrlFailed(e)); return Left(SaveRecentLoginUrlFailed(e));
} }
} }
} }
@@ -9,12 +9,12 @@ class SaveLoginUsernameOnMobileInteractor {
SaveLoginUsernameOnMobileInteractor(this.loginUsernameRepository); SaveLoginUsernameOnMobileInteractor(this.loginUsernameRepository);
Stream<Either<Failure, Success>> execute(RecentLoginUsername recentLoginUsername) async* { Future<Either<Failure, Success>> execute(RecentLoginUsername recentLoginUsername) async {
try { try {
await loginUsernameRepository.saveLoginUsername(recentLoginUsername); await loginUsernameRepository.saveLoginUsername(recentLoginUsername);
yield Right(SaveRecentLoginUsernameSuccess()); return Right(SaveRecentLoginUsernameSuccess());
} catch(exception) { } catch(exception) {
yield Left(SaveRecentLoginUsernameFailed(exception)); return Left(SaveRecentLoginUsernameFailed(exception));
} }
} }
} }