Skip to content

Commit ce12100

Browse files
chore: separate exception handlings
1 parent 1203c21 commit ce12100

File tree

3 files changed

+55
-75
lines changed

3 files changed

+55
-75
lines changed

flutter-ory-network/lib/blocs/auth/auth_bloc.dart

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,16 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
3737
isLoading: false,
3838
status: AuthStatus.authenticated,
3939
session: session));
40-
} on CustomException catch (e) {
41-
if (e case UnauthorizedException _) {
42-
emit(state.copyWith(
43-
status: AuthStatus.unauthenticated,
44-
session: null,
45-
isLoading: false));
46-
} else if (e case TwoFactorAuthRequiredException _) {
47-
emit(state.copyWith(
48-
isLoading: false, session: null, status: AuthStatus.aal2Requested));
49-
} else if (e case UnknownException _) {
50-
emit(state.copyWith(isLoading: false, errorMessage: e.message));
51-
} else {
52-
emit(state.copyWith(isLoading: false));
53-
}
40+
} on UnauthorizedException catch (_) {
41+
emit(state.copyWith(
42+
status: AuthStatus.unauthenticated, session: null, isLoading: false));
43+
} on TwoFactorAuthRequiredException catch (_) {
44+
emit(state.copyWith(
45+
isLoading: false, session: null, status: AuthStatus.aal2Requested));
46+
} on UnknownException catch (e) {
47+
emit(state.copyWith(isLoading: false, errorMessage: e.message));
48+
} catch (_) {
49+
emit(state.copyWith(isLoading: false));
5450
}
5551
}
5652

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

6359
emit(state.copyWith(
6460
isLoading: false, status: AuthStatus.unauthenticated, session: null));
65-
} on CustomException catch (e) {
66-
if (e case UnauthorizedException _) {
67-
emit(state.copyWith(
68-
isLoading: false,
69-
status: AuthStatus.unauthenticated,
70-
session: null));
71-
} else if (e case UnknownException _) {
72-
emit(state.copyWith(isLoading: false, errorMessage: e.message));
73-
} else {
74-
emit(state.copyWith(isLoading: false));
75-
}
61+
} on UnauthorizedException catch (_) {
62+
emit(state.copyWith(
63+
status: AuthStatus.unauthenticated, session: null, isLoading: false));
64+
} on UnknownException catch (e) {
65+
emit(state.copyWith(isLoading: false, errorMessage: e.message));
66+
} catch (_) {
67+
emit(state.copyWith(isLoading: false));
7668
}
7769
}
7870
}

flutter-ory-network/lib/blocs/login/login_bloc.dart

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
1818
final AuthBloc authBloc;
1919
final AuthRepository repository;
2020
LoginBloc({required this.authBloc, required this.repository})
21-
: super(LoginState()) {
21+
: super(const LoginState()) {
2222
on<CreateLoginFlow>(_onCreateLoginFlow);
2323
on<GetLoginFlow>(_onGetLoginFlow);
2424
on<ChangeNodeValue>(_onChangeNodeValue);
@@ -33,12 +33,10 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
3333
final loginFlow = await repository.createLoginFlow(aal: event.aal);
3434

3535
emit(state.copyWith(loginFlow: loginFlow, isLoading: false));
36-
} on CustomException catch (e) {
37-
if (e case UnknownException _) {
38-
emit(state.copyWith(isLoading: false, message: e.message));
39-
} else {
40-
emit(state.copyWith(isLoading: false));
41-
}
36+
} on UnknownException catch (e) {
37+
emit(state.copyWith(isLoading: false, message: e.message));
38+
} catch (_) {
39+
emit(state.copyWith(isLoading: false));
4240
}
4341
}
4442

@@ -48,12 +46,10 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
4846
emit(state.copyWith(isLoading: true, message: null));
4947
final loginFlow = await repository.getLoginFlow(flowId: event.flowId);
5048
emit(state.copyWith(loginFlow: loginFlow, isLoading: false));
51-
} on CustomException catch (e) {
52-
if (e case UnknownException _) {
53-
emit(state.copyWith(isLoading: false, message: e.message));
54-
} else {
55-
emit(state.copyWith(isLoading: false));
56-
}
49+
} on UnknownException catch (e) {
50+
emit(state.copyWith(isLoading: false, message: e.message));
51+
} catch (_) {
52+
emit(state.copyWith(isLoading: false));
5753
}
5854
}
5955

@@ -75,20 +71,18 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
7571
value: event.value,
7672
nodes: state.loginFlow!.ui.nodes.toList());
7773
authBloc.add(ChangeAuthStatus(status: AuthStatus.authenticated));
78-
} on CustomException catch (e) {
79-
if (e case BadRequestException<LoginFlow> _) {
80-
emit(state.copyWith(loginFlow: e.flow, isLoading: false));
81-
} else if (e case UnauthorizedException _) {
82-
authBloc.add(ChangeAuthStatus(status: AuthStatus.unauthenticated));
83-
} else if (e case FlowExpiredException _) {
84-
add(GetLoginFlow(flowId: e.flowId));
85-
} else if (e case TwoFactorAuthRequiredException _) {
86-
authBloc.add(ChangeAuthStatus(status: AuthStatus.aal2Requested));
87-
} else if (e case UnknownException _) {
88-
emit(state.copyWith(isLoading: false, message: e.message));
89-
} else {
90-
emit(state.copyWith(isLoading: false));
91-
}
74+
} on BadRequestException<LoginFlow> catch (e) {
75+
emit(state.copyWith(loginFlow: e.flow, isLoading: false));
76+
} on UnauthorizedException catch (_) {
77+
authBloc.add(ChangeAuthStatus(status: AuthStatus.unauthenticated));
78+
} on FlowExpiredException catch (e) {
79+
add(GetLoginFlow(flowId: e.flowId));
80+
} on TwoFactorAuthRequiredException catch (_) {
81+
authBloc.add(ChangeAuthStatus(status: AuthStatus.aal2Requested));
82+
} on UnknownException catch (e) {
83+
emit(state.copyWith(isLoading: false, message: e.message));
84+
} catch (_) {
85+
emit(state.copyWith(isLoading: false));
9286
}
9387
}
9488
}

flutter-ory-network/lib/blocs/registration/registration_bloc.dart

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class RegistrationBloc extends Bloc<RegistrationEvent, RegistrationState> {
1818
final AuthBloc authBloc;
1919
final AuthRepository repository;
2020
RegistrationBloc({required this.authBloc, required this.repository})
21-
: super(RegistrationState()) {
21+
: super(const RegistrationState()) {
2222
on<CreateRegistrationFlow>(_onCreateRegistrationFlow);
2323
on<GetRegistrationFlow>(_onGetRegistrationFlow);
2424
on<ChangeNodeValue>(_onChangeNodeValue);
@@ -31,12 +31,10 @@ class RegistrationBloc extends Bloc<RegistrationEvent, RegistrationState> {
3131
emit(state.copyWith(isLoading: true, message: null));
3232
final flow = await repository.createRegistrationFlow();
3333
emit(state.copyWith(registrationFlow: flow, isLoading: false));
34-
} on CustomException catch (e) {
35-
if (e case UnknownException _) {
36-
emit(state.copyWith(isLoading: false, message: e.message));
37-
} else {
38-
emit(state.copyWith(isLoading: false));
39-
}
34+
} on UnknownException catch (e) {
35+
emit(state.copyWith(isLoading: false, message: e.message));
36+
} catch (_) {
37+
emit(state.copyWith(isLoading: false));
4038
}
4139
}
4240

@@ -46,12 +44,10 @@ class RegistrationBloc extends Bloc<RegistrationEvent, RegistrationState> {
4644
emit(state.copyWith(isLoading: true, message: null));
4745
final flow = await repository.getRegistrationFlow(flowId: event.flowId);
4846
emit(state.copyWith(registrationFlow: flow, isLoading: false));
49-
} on CustomException catch (e) {
50-
if (e case UnknownException _) {
51-
emit(state.copyWith(isLoading: false, message: e.message));
52-
} else {
53-
emit(state.copyWith(isLoading: false));
54-
}
47+
} on UnknownException catch (e) {
48+
emit(state.copyWith(isLoading: false, message: e.message));
49+
} catch (_) {
50+
emit(state.copyWith(isLoading: false));
5551
}
5652
}
5753

@@ -79,16 +75,14 @@ class RegistrationBloc extends Bloc<RegistrationEvent, RegistrationState> {
7975
nodes: state.registrationFlow!.ui.nodes.toList());
8076
authBloc.add(ChangeAuthStatus(status: AuthStatus.authenticated));
8177
}
82-
} on CustomException catch (e) {
83-
if (e case BadRequestException<RegistrationFlow> _) {
84-
emit(state.copyWith(registrationFlow: e.flow, isLoading: false));
85-
} else if (e case FlowExpiredException _) {
86-
add(GetRegistrationFlow(flowId: e.flowId));
87-
} else if (e case UnknownException _) {
88-
emit(state.copyWith(isLoading: false, message: e.message));
89-
} else {
90-
emit(state.copyWith(isLoading: false));
91-
}
78+
} on BadRequestException<RegistrationFlow> catch (e) {
79+
emit(state.copyWith(registrationFlow: e.flow, isLoading: false));
80+
} on FlowExpiredException catch (e) {
81+
add(GetRegistrationFlow(flowId: e.flowId));
82+
} on UnknownException catch (e) {
83+
emit(state.copyWith(isLoading: false, message: e.message));
84+
} catch (_) {
85+
emit(state.copyWith(isLoading: false));
9286
}
9387
}
9488
}

0 commit comments

Comments
 (0)