Refactor API error handling and add try-catch blocks

Added try-catch blocks for error handling in API's files to rethrow the errors to the cubit so cubits can update the UI based on them.

Refactored error handling in HTTPInterceptor and HTTPService classes.
This commit is contained in:
Mohammad Salameh
2024-04-15 15:47:13 +03:00
parent dd90a2133f
commit df13c66b1a
9 changed files with 247 additions and 191 deletions

View File

@ -125,14 +125,17 @@ class HomeCubit extends Cubit<HomeState> {
emitSafe(GetSpacesLoading());
try {
spaces = await SpacesAPI.getSpaces();
selectedSpace = spaces!.isNotEmpty
?
// selectSpace(spaces!.first)
selectedSpace = spaces!.first
: null;
emitSafe(GetSpacesLoaded(spaces!));
} on ServerFailure catch (failure) {
emitSafe(GetSpacesError(failure.errMessage));
} catch (failure) {
emitSafe(GetSpacesError(failure.toString()));
return;
}
if (spaces != null && spaces!.isNotEmpty) {
selectedSpace = spaces!.first;
emitSafe(GetSpacesSuccess(spaces!));
fetchRooms(selectedSpace!);
} else {
emitSafe(GetSpacesError("No spaces found"));
}
}
@ -140,13 +143,14 @@ class HomeCubit extends Cubit<HomeState> {
emitSafe(GetSpaceRoomsLoading());
try {
space.rooms = await SpacesAPI.getRoomsBySpaceId(space.id!);
if (space.rooms != null) {
emitSafe(GetSpaceRoomsLoaded(space.rooms!));
} else {
emitSafe(GetSpaceRoomsError("No rooms found"));
}
} on ServerFailure catch (failure) {
emitSafe(GetSpacesError(failure.errMessage));
} catch (failure) {
emitSafe(GetSpaceRoomsError(failure.toString()));
return;
}
if (space.rooms != null) {
emitSafe(GetSpaceRoomsSuccess(space.rooms!));
} else {
emitSafe(GetSpaceRoomsError("No rooms found"));
}
}
@ -169,14 +173,6 @@ class HomeCubit extends Cubit<HomeState> {
),
],
'Devices': [
// IconButton(
// icon: Image.asset(
// Assets.iconsFilter,
// height: 20,
// width: 20,
// ),
// onPressed: () {},
// ),
IconButton(
icon: const Icon(
Icons.add,