mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-27 05:57:56 +00:00
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:
@@ -251,32 +251,42 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
code: control.code,
|
||||
));
|
||||
try {
|
||||
await DevicesAPI.controlDevice(control).then((response) {
|
||||
emitSafe(DeviceControlSuccess(
|
||||
code: control.code,
|
||||
));
|
||||
if (response['success'] ?? false) {
|
||||
Future.delayed(const Duration(milliseconds: 400), () {
|
||||
getDevicesStatues(
|
||||
deviceId,
|
||||
HomeCubit.getInstance().selectedSpace!.rooms!.indexOf(
|
||||
HomeCubit.getInstance().selectedRoom!,
|
||||
),
|
||||
code: control.code);
|
||||
});
|
||||
} else {
|
||||
emitSafe(DeviceControlError('Failed to control the device'));
|
||||
}
|
||||
});
|
||||
} on ServerFailure catch (failure) {
|
||||
emitSafe(DeviceControlError(failure.errMessage));
|
||||
var response = await DevicesAPI.controlDevice(control);
|
||||
|
||||
if (response['success'] ?? false) {
|
||||
emitSafe(DeviceControlSuccess(code: control.code));
|
||||
//this delay is to give tuya server time to update the status
|
||||
Future.delayed(const Duration(milliseconds: 400), () {
|
||||
getDevicesStatues(
|
||||
deviceId,
|
||||
HomeCubit.getInstance()
|
||||
.selectedSpace!
|
||||
.rooms!
|
||||
.indexOf(HomeCubit.getInstance().selectedRoom!),
|
||||
code: control.code);
|
||||
});
|
||||
} else {
|
||||
emitSafe(DeviceControlError('Failed to control the device'));
|
||||
}
|
||||
} catch (failure) {
|
||||
emitSafe(DeviceControlError(failure.toString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fetchGroups(int spaceId) async {
|
||||
emitSafe(DevicesCategoriesLoading());
|
||||
allCategories = await DevicesAPI.fetchGroups(spaceId);
|
||||
emitSafe(DevicesCategoriesSuccess());
|
||||
try {
|
||||
allCategories = await DevicesAPI.fetchGroups(spaceId);
|
||||
} catch (e) {
|
||||
emitSafe(DevicesCategoriesError(e.toString()));
|
||||
return;
|
||||
}
|
||||
if (allCategories!.isNotEmpty && allCategories != null) {
|
||||
emitSafe(DevicesCategoriesSuccess());
|
||||
} else {
|
||||
emitSafe(DevicesCategoriesError('No Groups found'));
|
||||
}
|
||||
}
|
||||
|
||||
fetchDevicesByRoomId(int? roomId) async {
|
||||
@@ -287,46 +297,46 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
.selectedSpace!
|
||||
.rooms!
|
||||
.indexWhere((element) => element.id == roomId);
|
||||
HomeCubit.getInstance().selectedSpace!.rooms![roomIndex].devices =
|
||||
await SpacesAPI.getDevicesByRoomId(roomId);
|
||||
try {
|
||||
HomeCubit.getInstance().selectedSpace!.rooms![roomIndex].devices =
|
||||
await DevicesAPI.getDevicesByRoomId(roomId);
|
||||
} catch (e) {
|
||||
emitSafe(GetDevicesError(e.toString()));
|
||||
return;
|
||||
}
|
||||
emitSafe(GetDevicesSuccess());
|
||||
|
||||
//get status for each device
|
||||
//TODO get devices status per page via page controller instead of getting all devices status at once
|
||||
for (var device
|
||||
in HomeCubit.getInstance().selectedSpace!.rooms![roomIndex].devices!) {
|
||||
getDevicesStatues(device.id!, roomIndex);
|
||||
}
|
||||
|
||||
emitSafe(GetDevicesSuccess());
|
||||
}
|
||||
|
||||
getDevicesStatues(String deviceId, int roomIndex, {String? code}) async {
|
||||
emitSafe(GetDeviceStatusLoading(code: code));
|
||||
int deviceIndex = HomeCubit.getInstance()
|
||||
.selectedSpace!
|
||||
.rooms![roomIndex]
|
||||
.devices!
|
||||
.indexWhere((element) => element.id == deviceId);
|
||||
List<StatusModel> statuses = [];
|
||||
try {
|
||||
emitSafe(GetDeviceStatusLoading(code: code));
|
||||
int deviceIndex = HomeCubit.getInstance()
|
||||
.selectedSpace!
|
||||
.rooms![roomIndex]
|
||||
.devices!
|
||||
.indexWhere((element) => element.id == deviceId);
|
||||
|
||||
List<StatusModel> statuses = [];
|
||||
var response = await DevicesAPI.getDeviceStatus(deviceId);
|
||||
// if (response['result']['status'].length > 4)
|
||||
// print('response : ${response['result']['status'][4]}');
|
||||
|
||||
for (var status in response['result']['status']) {
|
||||
statuses.add(StatusModel.fromJson(status));
|
||||
}
|
||||
|
||||
HomeCubit.getInstance()
|
||||
.selectedSpace!
|
||||
.rooms![roomIndex]
|
||||
.devices![deviceIndex]
|
||||
.status = statuses;
|
||||
emitSafe(GetDeviceStatusSuccess(code: code));
|
||||
} on ServerFailure catch (failure) {
|
||||
emitSafe(
|
||||
GetDeviceStatusError(failure.errMessage),
|
||||
);
|
||||
} catch (e) {
|
||||
emitSafe(GetDeviceStatusError(e.toString()));
|
||||
return;
|
||||
}
|
||||
HomeCubit.getInstance()
|
||||
.selectedSpace!
|
||||
.rooms![roomIndex]
|
||||
.devices![deviceIndex]
|
||||
.status = statuses;
|
||||
emitSafe(GetDeviceStatusSuccess(code: code));
|
||||
}
|
||||
|
||||
///Lights
|
||||
|
||||
Reference in New Issue
Block a user