Skip to content

Commit

Permalink
chore: separate exception handlings
Browse files Browse the repository at this point in the history
  • Loading branch information
sashatalalasha committed Oct 16, 2023
1 parent 1203c21 commit ce12100
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 75 deletions.
42 changes: 17 additions & 25 deletions flutter-ory-network/lib/blocs/auth/auth_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,16 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
isLoading: false,
status: AuthStatus.authenticated,
session: session));
} on CustomException catch (e) {
if (e case UnauthorizedException _) {
emit(state.copyWith(
status: AuthStatus.unauthenticated,
session: null,
isLoading: false));
} else if (e case TwoFactorAuthRequiredException _) {
emit(state.copyWith(
isLoading: false, session: null, status: AuthStatus.aal2Requested));
} else if (e case UnknownException _) {
emit(state.copyWith(isLoading: false, errorMessage: e.message));
} else {
emit(state.copyWith(isLoading: false));
}
} on UnauthorizedException catch (_) {
emit(state.copyWith(
status: AuthStatus.unauthenticated, session: null, isLoading: false));
} on TwoFactorAuthRequiredException catch (_) {
emit(state.copyWith(
isLoading: false, session: null, status: AuthStatus.aal2Requested));
} on UnknownException catch (e) {
emit(state.copyWith(isLoading: false, errorMessage: e.message));
} catch (_) {
emit(state.copyWith(isLoading: false));
}
}

Expand All @@ -62,17 +58,13 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {

emit(state.copyWith(
isLoading: false, status: AuthStatus.unauthenticated, session: null));
} on CustomException catch (e) {
if (e case UnauthorizedException _) {
emit(state.copyWith(
isLoading: false,
status: AuthStatus.unauthenticated,
session: null));
} else if (e case UnknownException _) {
emit(state.copyWith(isLoading: false, errorMessage: e.message));
} else {
emit(state.copyWith(isLoading: false));
}
} on UnauthorizedException catch (_) {
emit(state.copyWith(
status: AuthStatus.unauthenticated, session: null, isLoading: false));
} on UnknownException catch (e) {
emit(state.copyWith(isLoading: false, errorMessage: e.message));
} catch (_) {
emit(state.copyWith(isLoading: false));
}
}
}
48 changes: 21 additions & 27 deletions flutter-ory-network/lib/blocs/login/login_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
final AuthBloc authBloc;
final AuthRepository repository;
LoginBloc({required this.authBloc, required this.repository})
: super(LoginState()) {
: super(const LoginState()) {
on<CreateLoginFlow>(_onCreateLoginFlow);
on<GetLoginFlow>(_onGetLoginFlow);
on<ChangeNodeValue>(_onChangeNodeValue);
Expand All @@ -33,12 +33,10 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
final loginFlow = await repository.createLoginFlow(aal: event.aal);

emit(state.copyWith(loginFlow: loginFlow, isLoading: false));
} on CustomException catch (e) {
if (e case UnknownException _) {
emit(state.copyWith(isLoading: false, message: e.message));
} else {
emit(state.copyWith(isLoading: false));
}
} on UnknownException catch (e) {
emit(state.copyWith(isLoading: false, message: e.message));
} catch (_) {
emit(state.copyWith(isLoading: false));
}
}

Expand All @@ -48,12 +46,10 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
emit(state.copyWith(isLoading: true, message: null));
final loginFlow = await repository.getLoginFlow(flowId: event.flowId);
emit(state.copyWith(loginFlow: loginFlow, isLoading: false));
} on CustomException catch (e) {
if (e case UnknownException _) {
emit(state.copyWith(isLoading: false, message: e.message));
} else {
emit(state.copyWith(isLoading: false));
}
} on UnknownException catch (e) {
emit(state.copyWith(isLoading: false, message: e.message));
} catch (_) {
emit(state.copyWith(isLoading: false));
}
}

Expand All @@ -75,20 +71,18 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
value: event.value,
nodes: state.loginFlow!.ui.nodes.toList());
authBloc.add(ChangeAuthStatus(status: AuthStatus.authenticated));
} on CustomException catch (e) {
if (e case BadRequestException<LoginFlow> _) {
emit(state.copyWith(loginFlow: e.flow, isLoading: false));
} else if (e case UnauthorizedException _) {
authBloc.add(ChangeAuthStatus(status: AuthStatus.unauthenticated));
} else if (e case FlowExpiredException _) {
add(GetLoginFlow(flowId: e.flowId));
} else if (e case TwoFactorAuthRequiredException _) {
authBloc.add(ChangeAuthStatus(status: AuthStatus.aal2Requested));
} else if (e case UnknownException _) {
emit(state.copyWith(isLoading: false, message: e.message));
} else {
emit(state.copyWith(isLoading: false));
}
} on BadRequestException<LoginFlow> catch (e) {
emit(state.copyWith(loginFlow: e.flow, isLoading: false));
} on UnauthorizedException catch (_) {
authBloc.add(ChangeAuthStatus(status: AuthStatus.unauthenticated));
} on FlowExpiredException catch (e) {
add(GetLoginFlow(flowId: e.flowId));
} on TwoFactorAuthRequiredException catch (_) {
authBloc.add(ChangeAuthStatus(status: AuthStatus.aal2Requested));
} on UnknownException catch (e) {
emit(state.copyWith(isLoading: false, message: e.message));
} catch (_) {
emit(state.copyWith(isLoading: false));
}
}
}
40 changes: 17 additions & 23 deletions flutter-ory-network/lib/blocs/registration/registration_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RegistrationBloc extends Bloc<RegistrationEvent, RegistrationState> {
final AuthBloc authBloc;
final AuthRepository repository;
RegistrationBloc({required this.authBloc, required this.repository})
: super(RegistrationState()) {
: super(const RegistrationState()) {
on<CreateRegistrationFlow>(_onCreateRegistrationFlow);
on<GetRegistrationFlow>(_onGetRegistrationFlow);
on<ChangeNodeValue>(_onChangeNodeValue);
Expand All @@ -31,12 +31,10 @@ class RegistrationBloc extends Bloc<RegistrationEvent, RegistrationState> {
emit(state.copyWith(isLoading: true, message: null));
final flow = await repository.createRegistrationFlow();
emit(state.copyWith(registrationFlow: flow, isLoading: false));
} on CustomException catch (e) {
if (e case UnknownException _) {
emit(state.copyWith(isLoading: false, message: e.message));
} else {
emit(state.copyWith(isLoading: false));
}
} on UnknownException catch (e) {
emit(state.copyWith(isLoading: false, message: e.message));
} catch (_) {
emit(state.copyWith(isLoading: false));
}
}

Expand All @@ -46,12 +44,10 @@ class RegistrationBloc extends Bloc<RegistrationEvent, RegistrationState> {
emit(state.copyWith(isLoading: true, message: null));
final flow = await repository.getRegistrationFlow(flowId: event.flowId);
emit(state.copyWith(registrationFlow: flow, isLoading: false));
} on CustomException catch (e) {
if (e case UnknownException _) {
emit(state.copyWith(isLoading: false, message: e.message));
} else {
emit(state.copyWith(isLoading: false));
}
} on UnknownException catch (e) {
emit(state.copyWith(isLoading: false, message: e.message));
} catch (_) {
emit(state.copyWith(isLoading: false));
}
}

Expand Down Expand Up @@ -79,16 +75,14 @@ class RegistrationBloc extends Bloc<RegistrationEvent, RegistrationState> {
nodes: state.registrationFlow!.ui.nodes.toList());
authBloc.add(ChangeAuthStatus(status: AuthStatus.authenticated));
}
} on CustomException catch (e) {
if (e case BadRequestException<RegistrationFlow> _) {
emit(state.copyWith(registrationFlow: e.flow, isLoading: false));
} else if (e case FlowExpiredException _) {
add(GetRegistrationFlow(flowId: e.flowId));
} else if (e case UnknownException _) {
emit(state.copyWith(isLoading: false, message: e.message));
} else {
emit(state.copyWith(isLoading: false));
}
} on BadRequestException<RegistrationFlow> catch (e) {
emit(state.copyWith(registrationFlow: e.flow, isLoading: false));
} on FlowExpiredException catch (e) {
add(GetRegistrationFlow(flowId: e.flowId));
} on UnknownException catch (e) {
emit(state.copyWith(isLoading: false, message: e.message));
} catch (_) {
emit(state.copyWith(isLoading: false));
}
}
}

0 comments on commit ce12100

Please sign in to comment.