mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
Implemented proper error handling.
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
|
import 'package:dio/dio.dart';
|
||||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
|
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
|
||||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/params/load_communities_param.dart';
|
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/params/load_communities_param.dart';
|
||||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/services/communities_service.dart';
|
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/services/communities_service.dart';
|
||||||
|
import 'package:syncrow_web/services/api/api_exception.dart';
|
||||||
import 'package:syncrow_web/services/api/http_service.dart';
|
import 'package:syncrow_web/services/api/http_service.dart';
|
||||||
|
|
||||||
class RemoteCommunitiesService implements CommunitiesService {
|
class RemoteCommunitiesService implements CommunitiesService {
|
||||||
@ -8,13 +10,25 @@ class RemoteCommunitiesService implements CommunitiesService {
|
|||||||
|
|
||||||
final HTTPService _httpService;
|
final HTTPService _httpService;
|
||||||
|
|
||||||
|
static const _defaultErrorMessage = 'Failed to load communities';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<List<CommunityModel>> getCommunity(LoadCommunitiesParam param) async {
|
Future<List<CommunityModel>> getCommunity(LoadCommunitiesParam param) async {
|
||||||
|
try {
|
||||||
return _httpService.get(
|
return _httpService.get(
|
||||||
path: '/api/communities/',
|
path: '/api/communities/',
|
||||||
expectedResponseModel: (json) => (json as List<dynamic>)
|
expectedResponseModel: (json) => (json as List<dynamic>)
|
||||||
.map((e) => CommunityModel.fromJson(e as Map<String, dynamic>))
|
.map((e) => CommunityModel.fromJson(e as Map<String, dynamic>))
|
||||||
.toList(),
|
.toList(),
|
||||||
);
|
);
|
||||||
|
} on DioException catch (e) {
|
||||||
|
final message = e.response?.data as Map<String, dynamic>?;
|
||||||
|
final error = message?['error'] as Map<String, dynamic>?;
|
||||||
|
final errorMessage = error?['error'] as String? ?? '';
|
||||||
|
throw APIException(errorMessage);
|
||||||
|
} catch (e) {
|
||||||
|
final formattedErrorMessage = [_defaultErrorMessage, '$e'].join(': ');
|
||||||
|
throw APIException(formattedErrorMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,9 +35,10 @@ class RemoteDeviceService implements DeviceService {
|
|||||||
final message = e.response?.data as Map<String, dynamic>?;
|
final message = e.response?.data as Map<String, dynamic>?;
|
||||||
final error = message?['error'] as Map<String, dynamic>?;
|
final error = message?['error'] as Map<String, dynamic>?;
|
||||||
final errorMessage = error?['error'] as String? ?? '';
|
final errorMessage = error?['error'] as String? ?? '';
|
||||||
final formattedErrorMessage = [_defaultErrorMessage, errorMessage].join(
|
final formattedErrorMessage = [
|
||||||
': ',
|
_defaultErrorMessage,
|
||||||
);
|
errorMessage,
|
||||||
|
].join(': ');
|
||||||
throw APIException(formattedErrorMessage);
|
throw APIException(formattedErrorMessage);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
final formattedErrorMessage = [_defaultErrorMessage, '$e'].join(': ');
|
final formattedErrorMessage = [_defaultErrorMessage, '$e'].join(': ');
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
|
import 'package:dio/dio.dart';
|
||||||
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/domain/models/space_details_model.dart';
|
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/domain/models/space_details_model.dart';
|
||||||
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/domain/params/load_spaces_param.dart';
|
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/domain/params/load_spaces_param.dart';
|
||||||
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/domain/services/space_details_service.dart';
|
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/domain/services/space_details_service.dart';
|
||||||
|
import 'package:syncrow_web/services/api/api_exception.dart';
|
||||||
import 'package:syncrow_web/services/api/http_service.dart';
|
import 'package:syncrow_web/services/api/http_service.dart';
|
||||||
|
|
||||||
class RemoteSpaceDetailsService implements SpaceDetailsService {
|
class RemoteSpaceDetailsService implements SpaceDetailsService {
|
||||||
@ -10,6 +12,8 @@ class RemoteSpaceDetailsService implements SpaceDetailsService {
|
|||||||
required HTTPService httpService,
|
required HTTPService httpService,
|
||||||
}) : _httpService = httpService;
|
}) : _httpService = httpService;
|
||||||
|
|
||||||
|
static const _defaultErrorMessage = 'Failed to load space details';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<SpaceDetailsModel> getSpaceDetails(LoadSpacesParam param) async {
|
Future<SpaceDetailsModel> getSpaceDetails(LoadSpacesParam param) async {
|
||||||
try {
|
try {
|
||||||
@ -20,8 +24,17 @@ class RemoteSpaceDetailsService implements SpaceDetailsService {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
return response;
|
return response;
|
||||||
|
} on DioException catch (e) {
|
||||||
|
final message = e.response?.data as Map<String, dynamic>?;
|
||||||
|
final error = message?['error'] as Map<String, dynamic>?;
|
||||||
|
final errorMessage = error?['error'] as String? ?? '';
|
||||||
|
final formattedErrorMessage = [_defaultErrorMessage, errorMessage].join(
|
||||||
|
': ',
|
||||||
|
);
|
||||||
|
throw APIException(formattedErrorMessage);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw Exception('Failed to fetch space details: $e');
|
final formattedErrorMessage = [_defaultErrorMessage, '$e'].join(': ');
|
||||||
|
throw APIException(formattedErrorMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,9 +21,7 @@ class TagsBloc extends Bloc<TagsEvent, TagsState> {
|
|||||||
) async {
|
) async {
|
||||||
emit(TagsLoading());
|
emit(TagsLoading());
|
||||||
try {
|
try {
|
||||||
final tags = await _tagsService.loadTags(
|
final tags = await _tagsService.loadTags(event.param);
|
||||||
event.param,
|
|
||||||
);
|
|
||||||
emit(TagsLoaded(tags));
|
emit(TagsLoaded(tags));
|
||||||
} on APIException catch (e) {
|
} on APIException catch (e) {
|
||||||
emit(TagsFailure(e.message));
|
emit(TagsFailure(e.message));
|
||||||
|
Reference in New Issue
Block a user