Untitled
unknown
plain_text
2 years ago
6.2 kB
5
Indexable
import 'dart:io'; import 'package:ceramic_quotation/app/constant/app_string.dart'; import 'package:ceramic_quotation/app/modules/login/views/login_view.dart'; import 'package:dio/dio.dart'; import 'package:get/get.dart'; import '../common/progress_indicator.dart'; import '../constant/app_color.dart'; import '../constant/status_objects.dart'; import 'app_prefrence.dart'; Failure? getCurrentFailure(error) { hideLoadingIndicator(); if (error is Exception) { try { Failure? failure; if (error is DioError) { switch (error.type) { case DioErrorType.cancel: failure = Failure(error: error, errorMessage: AppString.requestCancel); break; case DioErrorType.connectTimeout: failure = Failure( error: error, errorMessage: AppString.cancelTimeout, ); break; case DioErrorType.other: failure = Failure(error: error, errorMessage: AppString.noInternet); break; case DioErrorType.receiveTimeout: failure = Failure( error: error, errorMessage: AppString.sendConnectionTimeout, ); break; case DioErrorType.sendTimeout: failure = Failure( error: error, errorMessage: AppString.sendConnectionTimeout, ); break; case DioErrorType.response: switch (error.response!.statusCode) { case 400: final errorMessage = _getErrorMessage(error); failure = Failure( error: error, errorMessage: errorMessage ?? AppString.badRequest, ); break; case 401: final errorMessage = _getErrorMessage(error); failure = Failure( error: error, errorMessage: errorMessage ?? AppString.unUnauthorisedRequest, ); break; case 403: final errorMessage = _getErrorMessage(error); failure = Failure( error: error, errorMessage: errorMessage ?? AppString.notAllowedArea, ); Get.deleteAll(); Get.offAll(LoginView()); break; case 404: final errorMessage = _getErrorMessage(error); failure = Failure( error: error, errorMessage: errorMessage ?? AppString.itemNotFound, ); break; case 408: final errorMessage = _getErrorMessage(error); failure = Failure( error: error, errorMessage: errorMessage ?? AppString.connectionReqTimeOut, ); break; case 409: final errorMessage = _getErrorMessage(error); failure = Failure( error: error, errorMessage: errorMessage ?? AppString.errorDuToaConflict, ); break; case 422: final errorMessage = _getErrorMessage(error); failure = Failure( error: error, errorMessage: errorMessage ?? AppString.unProcessableEntity, ); Get.snackbar( 'Failure', errorMessage.toString(), backgroundColor: AppColors.primaryBlack, colorText: AppColors.primaryWhite, duration: const Duration(seconds: 8), snackPosition: SnackPosition.TOP, ); break; case 429: AppPreference.clear(); Get.deleteAll(); Get.offAll(LoginView()); break; case 500: failure = Failure( error: error, errorMessage: AppString.internalServerError, ); break; case 503: failure = Failure( error: error, errorMessage: AppString.serviceUnavailable, ); break; default: final int? responseCode = error.response!.statusCode; failure = Failure( error: error, errorMessage: AppString.receivedInvalidCode + responseCode.toString(), ); } break; } } else if (error is SocketException) { failure = Failure(error: error, errorMessage: AppString.noInternet); } else if (error.toString().contains( AppString.isNotaSubType, )) { failure = _unableToProcessDataFailure(error); } else if (error is FormatException) { failure = _formateExceptionFailure(error); } else { failure = _unexpectedErrorFailure(error); } return failure; } on FormatException catch (e) { return _formateExceptionFailure(e); } catch (e) { return _unexpectedErrorFailure(error); } } else { if (error.toString().contains(AppString.isNotaSubType)) { return _unableToProcessDataFailure(error); } else { return _unexpectedErrorFailure(error); } } } Failure _unableToProcessDataFailure(error) => Failure(error: error, errorMessage: AppString.unableTopProcess); Failure _unexpectedErrorFailure(error) => Failure(error: error, errorMessage: AppString.unableTopProcess); Failure _formateExceptionFailure(FormatException error) { return Failure( error: error, errorMessage: AppString.dataNotExpected, ); } String? _getErrorMessage(error) { final response = error.response; if (response == null) return null; if (response.data is String) return response.data['error']; return response.data['error'] != null ? response.data['error']['message'] : response.data['message']; }
Editor is loading...