From ce12100a7ddaa455f1ad00b4c72b0dbd8fcc2e7f Mon Sep 17 00:00:00 2001 From: Alexandra Talalaieva <25621530+sashatalalasha@users.noreply.github.com> Date: Mon, 16 Oct 2023 16:41:39 +0200 Subject: [PATCH] chore: separate exception handlings --- .../lib/blocs/auth/auth_bloc.dart | 42 +++++++--------- .../lib/blocs/login/login_bloc.dart | 48 ++++++++----------- .../blocs/registration/registration_bloc.dart | 40 +++++++--------- 3 files changed, 55 insertions(+), 75 deletions(-) diff --git a/flutter-ory-network/lib/blocs/auth/auth_bloc.dart b/flutter-ory-network/lib/blocs/auth/auth_bloc.dart index 2fcdd21..6d49f03 100644 --- a/flutter-ory-network/lib/blocs/auth/auth_bloc.dart +++ b/flutter-ory-network/lib/blocs/auth/auth_bloc.dart @@ -37,20 +37,16 @@ class AuthBloc extends Bloc { 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)); } } @@ -62,17 +58,13 @@ class AuthBloc extends Bloc { 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)); } } } diff --git a/flutter-ory-network/lib/blocs/login/login_bloc.dart b/flutter-ory-network/lib/blocs/login/login_bloc.dart index d8c1584..103b574 100644 --- a/flutter-ory-network/lib/blocs/login/login_bloc.dart +++ b/flutter-ory-network/lib/blocs/login/login_bloc.dart @@ -18,7 +18,7 @@ class LoginBloc extends Bloc { final AuthBloc authBloc; final AuthRepository repository; LoginBloc({required this.authBloc, required this.repository}) - : super(LoginState()) { + : super(const LoginState()) { on(_onCreateLoginFlow); on(_onGetLoginFlow); on(_onChangeNodeValue); @@ -33,12 +33,10 @@ class LoginBloc extends Bloc { 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)); } } @@ -48,12 +46,10 @@ class LoginBloc extends Bloc { 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)); } } @@ -75,20 +71,18 @@ class LoginBloc extends Bloc { value: event.value, nodes: state.loginFlow!.ui.nodes.toList()); authBloc.add(ChangeAuthStatus(status: AuthStatus.authenticated)); - } on CustomException catch (e) { - if (e case BadRequestException _) { - 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 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)); } } } diff --git a/flutter-ory-network/lib/blocs/registration/registration_bloc.dart b/flutter-ory-network/lib/blocs/registration/registration_bloc.dart index d4f4224..cc861f0 100644 --- a/flutter-ory-network/lib/blocs/registration/registration_bloc.dart +++ b/flutter-ory-network/lib/blocs/registration/registration_bloc.dart @@ -18,7 +18,7 @@ class RegistrationBloc extends Bloc { final AuthBloc authBloc; final AuthRepository repository; RegistrationBloc({required this.authBloc, required this.repository}) - : super(RegistrationState()) { + : super(const RegistrationState()) { on(_onCreateRegistrationFlow); on(_onGetRegistrationFlow); on(_onChangeNodeValue); @@ -31,12 +31,10 @@ class RegistrationBloc extends Bloc { 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)); } } @@ -46,12 +44,10 @@ class RegistrationBloc extends Bloc { 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)); } } @@ -79,16 +75,14 @@ class RegistrationBloc extends Bloc { nodes: state.registrationFlow!.ui.nodes.toList()); authBloc.add(ChangeAuthStatus(status: AuthStatus.authenticated)); } - } on CustomException catch (e) { - if (e case BadRequestException _) { - 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 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)); } } }