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,
List<String> scopes);
Future<String?> getAuthenticationInfo();
Future<String> getAuthenticationInfo();
}
@@ -25,8 +25,7 @@ class AuthenticationOIDCDataSourceImpl extends AuthenticationOIDCDataSource {
@override
Future<OIDCResponse> checkOIDCIsAvailable(OIDCRequest oidcRequest) {
return Future.sync(() async {
final oidcResponse = await _oidcHttpClient.checkOIDCIsAvailable(oidcRequest);
return oidcResponse!;
return await _oidcHttpClient.checkOIDCIsAvailable(oidcRequest);
}).catchError(_exceptionThrower.throwException);
}
@@ -128,7 +127,7 @@ class AuthenticationOIDCDataSourceImpl extends AuthenticationOIDCDataSource {
}
@override
Future<String?> getAuthenticationInfo() {
Future<String> getAuthenticationInfo() {
return Future.sync(() async {
return await _authenticationClient.getAuthenticationInfo();
}).catchError(_exceptionThrower.throwException);
@@ -13,7 +13,7 @@ abstract class AuthenticationClientBase {
String discoveryUrl,
List<String> scopes);
Future<String?> getAuthenticationInfo();
Future<String> getAuthenticationInfo();
Future<TokenOIDC> getTokenOIDC(
String clientId,
@@ -93,8 +93,8 @@ class AuthenticationClientMobile implements AuthenticationClientBase {
}
@override
Future<String?> getAuthenticationInfo() {
return Future.value(null);
Future<String> getAuthenticationInfo() {
return Future.value('');
}
}
@@ -97,10 +97,14 @@ class AuthenticationClientWeb implements AuthenticationClientBase {
}
@override
Future<String?> getAuthenticationInfo() async {
Future<String> getAuthenticationInfo() async {
final authUrl = html.window.sessionStorage[OIDCConstant.authResponseKey];
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);
Future<OIDCResponse?> checkOIDCIsAvailable(OIDCRequest oidcRequest) async {
Future<OIDCResponse> checkOIDCIsAvailable(OIDCRequest oidcRequest) async {
final result = await _dioClient.get(
Endpoint.webFinger
.generateOIDCPath(Uri.parse(oidcRequest.baseUrl))
@@ -31,10 +31,14 @@ class OIDCHttpClient {
.generateEndpointPath()
);
log('OIDCHttpClient::checkOIDCIsAvailable(): RESULT: $result');
if (result is Map<String, dynamic>) {
return OIDCResponse.fromJson(result);
if (result != null) {
if (result is Map<String, dynamic>) {
return OIDCResponse.fromJson(result);
} else {
return OIDCResponse.fromJson(jsonDecode(result));
}
} else {
return OIDCResponse.fromJson(jsonDecode(result));
throw CanNotFoundOIDCLinks();
}
}
@@ -86,7 +86,7 @@ class AuthenticationOIDCRepositoryImpl extends AuthenticationOIDCRepository {
}
@override
Future<String?> getAuthenticationInfo() {
Future<String> getAuthenticationInfo() {
return _oidcDataSource.getAuthenticationInfo();
}
@@ -23,13 +23,9 @@ class BadGateway extends AuthenticationException {
List<Object?> get props => [message];
}
class NotFoundAuthenticatedAccountException implements Exception {
NotFoundAuthenticatedAccountException();
}
class NotFoundAuthenticatedAccountException implements Exception {}
class NotFoundStoredTokenException implements Exception {
NotFoundStoredTokenException();
}
class NotFoundStoredTokenException implements Exception {}
class InvalidBaseUrl extends AuthenticationException {
InvalidBaseUrl() : super(AuthenticationException.invalidBaseUrl);
@@ -38,17 +34,21 @@ class InvalidBaseUrl extends AuthenticationException {
List<Object?> get props => [message];
}
class NotFoundAccessTokenException implements Exception {
NotFoundAccessTokenException();
}
class NotFoundAccessTokenException implements Exception {}
class AccessTokenInvalidException implements Exception {
AccessTokenInvalidException();
}
class AccessTokenInvalidException implements Exception {}
class DownloadAttachmentHasTokenExpiredException implements Exception {
final String 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,
List<String> scopes);
Future<String?> getAuthenticationInfo();
Future<String> getAuthenticationInfo();
}
@@ -18,9 +18,8 @@ class AuthenticationUserSuccess extends UIState {
}
class AuthenticationUserFailure extends FeatureFailure {
final dynamic exception;
AuthenticationUserFailure(this.exception);
AuthenticationUserFailure(dynamic exception) : super(exception: exception);
@override
List<Object?> get props => [exception];
@@ -20,9 +20,8 @@ class CheckOIDCIsAvailableSuccess extends UIState {
}
class CheckOIDCIsAvailableFailure extends FeatureFailure {
final dynamic exception;
CheckOIDCIsAvailableFailure(this.exception);
CheckOIDCIsAvailableFailure(dynamic exception) : super(exception: exception);
@override
List<Object?> get props => [exception];
@@ -19,9 +19,8 @@ class GetAuthenticationInfoSuccess extends UIState {
}
class GetAuthenticationInfoFailure extends FeatureFailure {
final dynamic exception;
GetAuthenticationInfoFailure(this.exception);
GetAuthenticationInfoFailure(dynamic exception) : super(exception: exception);
@override
List<Object?> get props => [exception];
@@ -20,9 +20,8 @@ class GetOIDCConfigurationSuccess extends UIState {
}
class GetOIDCConfigurationFailure extends FeatureFailure {
final dynamic exception;
GetOIDCConfigurationFailure(this.exception);
GetOIDCConfigurationFailure(dynamic exception) : super(exception: exception);
@override
List<Object?> get props => [exception];
@@ -20,9 +20,8 @@ class GetOIDCIsAvailableSuccess extends UIState {
}
class GetOIDCIsAvailableFailure extends FeatureFailure {
final dynamic exception;
GetOIDCIsAvailableFailure(this.exception);
GetOIDCIsAvailableFailure(dynamic exception) : super(exception: exception);
@override
List<Object?> get props => [exception];
@@ -21,9 +21,8 @@ class GetTokenOIDCSuccess extends UIState {
}
class GetTokenOIDCFailure extends FeatureFailure {
final dynamic exception;
GetTokenOIDCFailure(this.exception);
GetTokenOIDCFailure(dynamic exception) : super(exception: exception);
@override
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/personal_account.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/authentication_repository.dart';
import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart';
@@ -23,20 +24,31 @@ class AuthenticationInteractor {
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 {
yield Right(AuthenticationUserLoading());
final user = await authenticationRepository.authenticationUser(baseUrl, userName, password);
await Future.wait([
credentialRepository.saveBaseUrl(baseUrl),
credentialRepository.storeAuthenticationInfo(AuthenticationInfoCache(userName.value, password.value)),
_accountRepository.setCurrentAccount(PersonalAccount(
userName.value,
AuthenticationType.basic,
isSelected: true
))
]);
yield Right(AuthenticationUserSuccess(user));
if (baseUrl != null && userName != null && password != null) {
final user = await authenticationRepository.authenticationUser(baseUrl, userName, password);
await Future.wait([
credentialRepository.saveBaseUrl(baseUrl),
credentialRepository.storeAuthenticationInfo(AuthenticationInfoCache(userName.value, password.value)),
_accountRepository.setCurrentAccount(PersonalAccount(
userName.value,
AuthenticationType.basic,
isSelected: true
))
]);
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) {
logError('AuthenticationInteractor::execute(): $e');
yield Left(AuthenticationUserFailure(e));
@@ -15,11 +15,7 @@ class GetAuthenticationInfoInteractor {
yield Right<Failure, Success>(GetAuthenticationInfoLoading());
final result = await _oidcRepository.getAuthenticationInfo();
log('GetAuthenticationInfoInteractor::execute(): result: $result');
if (result?.isNotEmpty == true) {
yield Right<Failure, Success>(GetAuthenticationInfoSuccess());
} else {
yield Left<Failure, Success>(GetAuthenticationInfoFailure(null));
}
yield Right<Failure, Success>(GetAuthenticationInfoSuccess());
} catch (e) {
log('GetAuthenticationInfoInteractor::execute(): ERROR: $e');
yield Left<Failure, Success>(GetAuthenticationInfoFailure(e));
@@ -10,12 +10,12 @@ class SaveLoginUrlOnMobileInteractor {
SaveLoginUrlOnMobileInteractor(this.loginUrlRepository);
Stream<Either<Failure, Success>> execute(RecentLoginUrl recentLoginUrl) async* {
Future<Either<Failure, Success>> execute(RecentLoginUrl recentLoginUrl) async {
try{
await loginUrlRepository.saveRecentLoginUrl(recentLoginUrl);
yield Right(SaveRecentLoginUrlSuccess());
return Right(SaveRecentLoginUrlSuccess());
} catch(e) {
yield Left(SaveRecentLoginUrlFailed(e));
return Left(SaveRecentLoginUrlFailed(e));
}
}
}
@@ -9,12 +9,12 @@ class SaveLoginUsernameOnMobileInteractor {
SaveLoginUsernameOnMobileInteractor(this.loginUsernameRepository);
Stream<Either<Failure, Success>> execute(RecentLoginUsername recentLoginUsername) async* {
Future<Either<Failure, Success>> execute(RecentLoginUsername recentLoginUsername) async {
try {
await loginUsernameRepository.saveLoginUsername(recentLoginUsername);
yield Right(SaveRecentLoginUsernameSuccess());
return Right(SaveRecentLoginUsernameSuccess());
} catch(exception) {
yield Left(SaveRecentLoginUsernameFailed(exception));
return Left(SaveRecentLoginUsernameFailed(exception));
}
}
}