handle method

Failure handle({
  1. RequestOptions? request,
  2. dynamic exception,
  3. Response? response,
})

Implementation

Failure handle({
  RequestOptions? request,
  dynamic exception,
  Response? response,
}) {
  final failureInfo = FailureInfo(
    request: request,
    exception: exception,
    response: response,
  );
  Failure? failure;
  if (exception is ErrorException) {
    failure = ErrorFailure(
      errorStatus: _statusChecker.getErrorState(exception.statusCode),
      error: exception.error,
    );
  } else if (exception is DioException) {
    switch (exception.type) {
      case DioExceptionType.connectionError:
      case DioExceptionType.badCertificate:
      case DioExceptionType.connectionTimeout:
      case DioExceptionType.sendTimeout:
      case DioExceptionType.receiveTimeout:
        failure = ConnectionFailure();
        break;
      case DioExceptionType.cancel:
        failure = RequestCanceledFailure();
        break;
      case DioExceptionType.badResponse:
      case DioExceptionType.unknown:
        {
          final socketException =
              exception.message?.contains('SocketException') ?? false;
          final getRequest = request?.method == 'GET';
          final httpException =
              (exception.message?.contains('HttpException') ?? false) ||
                  (exception.message?.contains('Connection') ?? false);
          if (socketException || (httpException && getRequest)) {
            failure = ConnectionFailure();
          } else {
            failure = UnkownFailure(failureInfo);
          }
        }
    }
  } else if (exception is ServerException) {
    final status = _statusChecker(exception.response?.statusCode);
    switch (status) {
      case HTTPCodes.invalidToken:
        failure = SessionEndedFailure();
        break;
      case HTTPCodes.serviceNotAvailable:
        failure = ServiceNotAvailableFailure(failureInfo);
        break;
      case HTTPCodes.unknown:
        failure = UnkownFailure(failureInfo);
        break;
      default:
        break;
    }
  } else if (exception is SocketException) {
    failure = ConnectionFailure();
  } else if (exception is FormatException ||
      exception is RangeError ||
      exception is TypeError ||
      exception is NoSuchMethodError) {
    failure = TypeFailure(failureInfo);
  } else if (exception is ValidationException) {
    failure = ValidationFailure(exception.value);
  }
  if (failure is ReportableFailure) {}
  return failure ?? UnkownFailure(failureInfo);
}