Skip to content

Commit c2935df

Browse files
committed
feat(authentication): adapt bloc to handle HtHttpException
Updates `AuthenticationBloc` to handle and emit `HtHttpException` objects instead of raw error strings. - All `catch` blocks now store the caught exception in the `exception` field of the `AuthenticationState`. - The manual email validation check is removed, as this logic is now handled by the `InvalidInputException` from the repository layer. - Generic `catch (e)` blocks now wrap the error in a `UnknownException` to ensure all failures are propagated as a standard exception type.
1 parent c5552d8 commit c2935df

File tree

1 file changed

+25
-48
lines changed

1 file changed

+25
-48
lines changed

lib/authentication/bloc/authentication_bloc.dart

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,7 @@ import 'dart:async';
33
import 'package:bloc/bloc.dart';
44
import 'package:equatable/equatable.dart';
55
import 'package:ht_auth_repository/ht_auth_repository.dart';
6-
import 'package:ht_shared/ht_shared.dart' show
7-
AuthenticationException,
8-
ForbiddenException,
9-
HtHttpException,
10-
InvalidInputException,
11-
NetworkException,
12-
NotFoundException,
13-
OperationFailedException,
14-
ServerException,
15-
UnauthorizedException,
16-
User;
6+
import 'package:ht_shared/ht_shared.dart' show AuthenticationException, ForbiddenException, HtHttpException, InvalidInputException, NetworkException, NotFoundException, OperationFailedException, ServerException, UnauthorizedException, UnknownException, User;
177

188
part 'authentication_event.dart';
199
part 'authentication_state.dart';
@@ -70,16 +60,6 @@ class AuthenticationBloc
7060
AuthenticationRequestSignInCodeRequested event,
7161
Emitter<AuthenticationState> emit,
7262
) async {
73-
// Validate email format (basic check)
74-
if (event.email.isEmpty || !event.email.contains('@')) {
75-
emit(
76-
state.copyWith(
77-
status: AuthenticationStatus.failure,
78-
errorMessage: 'Please enter a valid email address.',
79-
),
80-
);
81-
return;
82-
}
8363
emit(state.copyWith(status: AuthenticationStatus.requestCodeLoading));
8464
try {
8565
await _authenticationRepository.requestSignInCode(
@@ -96,61 +76,58 @@ class AuthenticationBloc
9676
emit(
9777
state.copyWith(
9878
status: AuthenticationStatus.failure,
99-
errorMessage: 'Invalid input: ${e.message}',
79+
exception: e,
10080
),
10181
);
10282
} on UnauthorizedException catch (e) {
10383
emit(
10484
state.copyWith(
10585
status: AuthenticationStatus.failure,
106-
errorMessage: e.message,
86+
exception: e,
10787
),
10888
);
10989
} on ForbiddenException catch (e) {
11090
emit(
11191
state.copyWith(
11292
status: AuthenticationStatus.failure,
113-
errorMessage: e.message,
93+
exception: e,
11494
),
11595
);
116-
} on NetworkException catch (_) {
96+
} on NetworkException catch (e) {
11797
emit(
11898
state.copyWith(
11999
status: AuthenticationStatus.failure,
120-
errorMessage: 'Network error occurred.',
100+
exception: e,
121101
),
122102
);
123103
} on ServerException catch (e) {
124104
emit(
125105
state.copyWith(
126106
status: AuthenticationStatus.failure,
127-
errorMessage: 'Server error: ${e.message}',
107+
exception: e,
128108
),
129109
);
130110
} on OperationFailedException catch (e) {
131111
emit(
132112
state.copyWith(
133113
status: AuthenticationStatus.failure,
134-
errorMessage: 'Operation failed: ${e.message}',
114+
exception: e,
135115
),
136116
);
137117
} on HtHttpException catch (e) {
138118
// Catch any other HtHttpException subtypes
139-
final message = e.message.isNotEmpty
140-
? e.message
141-
: 'An unspecified HTTP error occurred.';
142119
emit(
143120
state.copyWith(
144121
status: AuthenticationStatus.failure,
145-
errorMessage: 'HTTP error: $message',
122+
exception: e,
146123
),
147124
);
148125
} catch (e) {
149126
// Catch any other unexpected errors
150127
emit(
151128
state.copyWith(
152129
status: AuthenticationStatus.failure,
153-
errorMessage: 'An unexpected error occurred: $e',
130+
exception: UnknownException('An unexpected error occurred: $e'),
154131
),
155132
);
156133
// Optionally log the stackTrace here
@@ -175,58 +152,58 @@ class AuthenticationBloc
175152
emit(
176153
state.copyWith(
177154
status: AuthenticationStatus.failure,
178-
errorMessage: e.message,
155+
exception: e,
179156
),
180157
);
181158
} on AuthenticationException catch (e) {
182159
emit(
183160
state.copyWith(
184161
status: AuthenticationStatus.failure,
185-
errorMessage: e.message,
162+
exception: e,
186163
),
187164
);
188165
} on NotFoundException catch (e) {
189166
emit(
190167
state.copyWith(
191168
status: AuthenticationStatus.failure,
192-
errorMessage: e.message,
169+
exception: e,
193170
),
194171
);
195-
} on NetworkException catch (_) {
172+
} on NetworkException catch (e) {
196173
emit(
197174
state.copyWith(
198175
status: AuthenticationStatus.failure,
199-
errorMessage: 'Network error occurred.',
176+
exception: e,
200177
),
201178
);
202179
} on ServerException catch (e) {
203180
emit(
204181
state.copyWith(
205182
status: AuthenticationStatus.failure,
206-
errorMessage: 'Server error: ${e.message}',
183+
exception: e,
207184
),
208185
);
209186
} on OperationFailedException catch (e) {
210187
emit(
211188
state.copyWith(
212189
status: AuthenticationStatus.failure,
213-
errorMessage: 'Operation failed: ${e.message}',
190+
exception: e,
214191
),
215192
);
216193
} on HtHttpException catch (e) {
217194
// Catch any other HtHttpException subtypes
218195
emit(
219196
state.copyWith(
220197
status: AuthenticationStatus.failure,
221-
errorMessage: 'HTTP error: ${e.message}',
198+
exception: e,
222199
),
223200
);
224201
} catch (e) {
225202
// Catch any other unexpected errors
226203
emit(
227204
state.copyWith(
228205
status: AuthenticationStatus.failure,
229-
errorMessage: 'An unexpected error occurred: $e',
206+
exception: UnknownException('An unexpected error occurred: $e'),
230207
),
231208
);
232209
// Optionally log the stackTrace here
@@ -243,40 +220,40 @@ class AuthenticationBloc
243220
await _authenticationRepository.signOut();
244221
// On success, the _AuthenticationStatusChanged listener will handle
245222
// emitting AuthenticationUnauthenticated.
246-
} on NetworkException catch (_) {
223+
} on NetworkException catch (e) {
247224
emit(
248225
state.copyWith(
249226
status: AuthenticationStatus.failure,
250-
errorMessage: 'Network error occurred.',
227+
exception: e,
251228
),
252229
);
253230
} on ServerException catch (e) {
254231
emit(
255232
state.copyWith(
256233
status: AuthenticationStatus.failure,
257-
errorMessage: 'Server error: ${e.message}',
234+
exception: e,
258235
),
259236
);
260237
} on OperationFailedException catch (e) {
261238
emit(
262239
state.copyWith(
263240
status: AuthenticationStatus.failure,
264-
errorMessage: 'Operation failed: ${e.message}',
241+
exception: e,
265242
),
266243
);
267244
} on HtHttpException catch (e) {
268245
// Catch any other HtHttpException subtypes
269246
emit(
270247
state.copyWith(
271248
status: AuthenticationStatus.failure,
272-
errorMessage: 'HTTP error: ${e.message}',
249+
exception: e,
273250
),
274251
);
275252
} catch (e) {
276253
emit(
277254
state.copyWith(
278255
status: AuthenticationStatus.failure,
279-
errorMessage: 'An unexpected error occurred: $e',
256+
exception: UnknownException('An unexpected error occurred: $e'),
280257
),
281258
);
282259
}

0 commit comments

Comments
 (0)