You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
3.2 KiB
Dart
74 lines
3.2 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:hmg_patient_app_new/core/exceptions/api_exception.dart';
|
|
import 'package:hmg_patient_app_new/core/exceptions/api_failure.dart';
|
|
import 'package:hmg_patient_app_new/core/utils/loading_utils.dart';
|
|
import 'package:hmg_patient_app_new/extensions/route_extensions.dart';
|
|
import 'package:hmg_patient_app_new/services/dialog_service.dart';
|
|
import 'package:hmg_patient_app_new/services/logger_service.dart';
|
|
import 'package:hmg_patient_app_new/services/navigation_service.dart';
|
|
|
|
abstract class ErrorHandlerService {
|
|
Future<void> handleError({required Failure failure, Function() onOkPressed, Function(Failure)? onUnHandledFailure, Function(Failure)? onMessageStatusFailure});
|
|
}
|
|
|
|
class ErrorHandlerServiceImp implements ErrorHandlerService {
|
|
final DialogService dialogService;
|
|
final LoggerService loggerService;
|
|
final NavigationService navigationService;
|
|
|
|
ErrorHandlerServiceImp({
|
|
required this.dialogService,
|
|
required this.loggerService,
|
|
required this.navigationService,
|
|
});
|
|
|
|
@override
|
|
Future<void> handleError({required Failure failure, Function()? onOkPressed, Function(Failure)? onUnHandledFailure, Function(Failure)? onMessageStatusFailure}) async {
|
|
if (failure is APIException) {
|
|
loggerService.errorLogs("API Exception: ${failure.message}");
|
|
} else if (failure is ServerFailure) {
|
|
loggerService.errorLogs("Server Failure: ${failure.message}");
|
|
await _showDialog(failure);
|
|
} else if (failure is DataParsingFailure) {
|
|
loggerService.errorLogs("Data Parsing Failure: ${failure.message}");
|
|
await _showDialog(failure, title: "Data Error");
|
|
} else if (failure is StatusCodeFailure) {
|
|
loggerService.errorLogs("StatusCode Failure: ${failure.message}");
|
|
await _showDialog(failure, title: "Status Code Failure Error");
|
|
} else if (failure is HttpException) {
|
|
loggerService.errorLogs("Http Exception: ${failure.message}");
|
|
await _showDialog(failure, title: "Network Error");
|
|
} else if (failure is UnknownFailure) {
|
|
loggerService.errorLogs("Unknown Failure: ${failure.message}");
|
|
await _showDialog(failure, title: "Unknown Error");
|
|
} else if (failure is InvalidCredentials) {
|
|
loggerService.errorLogs("Invalid Credentials : ${failure.message}");
|
|
await _showDialog(failure, title: "Unknown Error");
|
|
} else if (failure is UserIntimationFailure) {
|
|
if (onUnHandledFailure != null) {
|
|
onUnHandledFailure(failure);
|
|
} else {
|
|
await _showDialog(failure, title: "Error", onOkPressed: onOkPressed);
|
|
}
|
|
} else if (failure is MessageStatusFailure) {
|
|
if (onMessageStatusFailure != null) {
|
|
onMessageStatusFailure(failure);
|
|
} else {
|
|
await _showDialog(failure, title: "Error", onOkPressed: onOkPressed);
|
|
}
|
|
} else {
|
|
loggerService.errorLogs("Unhandled failure type: $failure");
|
|
await _showDialog(failure, title: "Error", onOkPressed: onOkPressed);
|
|
}
|
|
}
|
|
|
|
Future<void> _showDialog(Failure failure, {String title = "Error", Function()? onOkPressed}) async {
|
|
if (LoadingUtils.isLoading) {
|
|
LoadingUtils.hideFullScreenLoader();
|
|
}
|
|
|
|
await dialogService.showErrorBottomSheet(message: failure.message, onOkPressed: onOkPressed);
|
|
}
|
|
}
|