feat: integrate ErrorService for consistent error display and standardize bloc error messages.

This commit is contained in:
Van Leemput Dayron
2025-12-02 13:59:40 +01:00
parent 1e70b9e09f
commit 6757cb013a
24 changed files with 927 additions and 608 deletions

View File

@@ -22,6 +22,7 @@
/// accountBloc.close();
/// ```
library;
import 'dart:async';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:travel_mate/services/error_service.dart';
@@ -30,7 +31,7 @@ import 'account_state.dart';
import '../../repositories/account_repository.dart';
import '../../models/account.dart';
class AccountBloc extends Bloc<AccountEvent, AccountState> {
class AccountBloc extends Bloc<AccountEvent, AccountState> {
final AccountRepository _repository;
StreamSubscription? _accountsSubscription;
final _errorService = ErrorService();
@@ -47,21 +48,27 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
Future<void> _onLoadAccountsByUserId(
LoadAccountsByUserId event,
Emitter<AccountState> emit,
) async {
) async {
try {
emit(AccountLoading());
await _accountsSubscription?.cancel();
_accountsSubscription = _repository.getAccountByUserId(event.userId).listen(
(accounts) {
add(_AccountsUpdated(accounts));
},
onError: (error) {
add(_AccountsUpdated([], error: error.toString()));
},
);
_accountsSubscription = _repository
.getAccountByUserId(event.userId)
.listen(
(accounts) {
add(_AccountsUpdated(accounts));
},
onError: (error) {
add(_AccountsUpdated([], error: error.toString()));
},
);
} catch (e, stackTrace) {
_errorService.logError(e.toString(), stackTrace);
emit(AccountError(e.toString()));
_errorService.logError(
'AccountBloc',
'Error loading accounts: $e',
stackTrace,
);
emit(const AccountError('Impossible de charger les comptes'));
}
}
@@ -89,8 +96,12 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
);
emit(AccountOperationSuccess('Compte créé avec succès. ID: $accountId'));
} catch (e, stackTrace) {
_errorService.logError(e.toString(), stackTrace);
emit(AccountError('Erreur lors de la création du compte: ${e.toString()}'));
_errorService.logError(
'AccountBloc',
'Error creating account: $e',
stackTrace,
);
emit(const AccountError('Impossible de créer le compte'));
}
}
@@ -98,7 +109,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
CreateAccountWithMembers event,
Emitter<AccountState> emit,
) async {
try{
try {
emit(AccountLoading());
final accountId = await _repository.createAccountWithMembers(
account: event.account,
@@ -106,8 +117,12 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
);
emit(AccountOperationSuccess('Compte créé avec succès. ID: $accountId'));
} catch (e, stackTrace) {
_errorService.logError(e.toString(), stackTrace);
emit(AccountError('Erreur lors de la création du compte: ${e.toString()}'));
_errorService.logError(
'AccountBloc',
'Error creating account with members: $e',
stackTrace,
);
emit(const AccountError('Impossible de créer le compte'));
}
}
@@ -120,8 +135,12 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
await _repository.addMemberToAccount(event.accountId, event.member);
emit(AccountOperationSuccess('Membre ajouté avec succès'));
} catch (e, stackTrace) {
_errorService.logError(e.toString(), stackTrace);
emit(AccountError('Erreur lors de l\'ajout du membre: ${e.toString()}'));
_errorService.logError(
'AccountBloc',
'Error adding member: $e',
stackTrace,
);
emit(const AccountError('Impossible d\'ajouter le membre'));
}
}
@@ -131,11 +150,18 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
) async {
try {
emit(AccountLoading());
await _repository.removeMemberFromAccount(event.accountId, event.memberId);
await _repository.removeMemberFromAccount(
event.accountId,
event.memberId,
);
emit(AccountOperationSuccess('Membre supprimé avec succès'));
} catch (e, stackTrace) {
_errorService.logError(e.toString(), stackTrace);
emit(AccountError('Erreur lors de la suppression du membre: ${e.toString()}'));
_errorService.logError(
'AccountBloc',
'Error removing member: $e',
stackTrace,
);
emit(const AccountError('Impossible de supprimer le membre'));
}
}
@@ -154,4 +180,4 @@ class _AccountsUpdated extends AccountEvent {
@override
List<Object?> get props => [accounts, error];
}
}