Skip to content

Commit a8592a4

Browse files
committed
refactor(user-management): add robust error handling to app role changes
This commit enhances the _onUserAppRoleChanged method in the UserManagementBloc by introducing a try-catch block and detailed logging. This change mirrors the robust implementation of _onUserDashboardRoleChanged, ensuring that any errors during the update process are caught, logged, and handled gracefully, preventing the BLoC's event stream from crashing and improving the overall stability of the user management feature.
1 parent 59f855c commit a8592a4

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

lib/user_management/bloc/user_management_bloc.dart

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,28 @@ class UserManagementBloc
181181
UserAppRoleChanged event,
182182
Emitter<UserManagementState> emit,
183183
) async {
184-
final userToUpdate = state.users.firstWhere((u) => u.id == event.userId);
185-
await _usersRepository.update(
186-
id: event.userId,
187-
item: userToUpdate.copyWith(appRole: event.appRole),
184+
_logger.info(
185+
'Attempting to change app role for user: ${event.userId} '
186+
'to ${event.appRole.name}',
188187
);
188+
try {
189+
final userToUpdate = state.users.firstWhere((u) => u.id == event.userId);
190+
_logger.info('Found user in state: $userToUpdate');
191+
192+
final updatedItem = userToUpdate.copyWith(appRole: event.appRole);
193+
_logger.info('Sending updated user object to repository: $updatedItem');
194+
195+
await _usersRepository.update(
196+
id: event.userId,
197+
item: updatedItem,
198+
);
199+
} catch (error, stackTrace) {
200+
_logger.severe(
201+
'Error changing user app role for ${event.userId}.',
202+
error,
203+
stackTrace,
204+
);
205+
addError(error, stackTrace);
206+
}
189207
}
190208
}

0 commit comments

Comments
 (0)