safeCall<F, T> method

  1. @protected
Future<Either<F, T>> safeCall<F, T>({
  1. required Future<T> call(),
  2. required F onNetworkError(),
  3. required F onResponseError(
    1. Response? response
    ),
  4. required F onUnknownError(
    1. Exception e
    ),
})

Implementation

@protected
Future<Either<F, T>> safeCall<F, T>({
  required Future<T> Function() call,
  required F Function() onNetworkError,
  required F Function(Response? response)
      onResponseError, // ignore: always_specify_types
  required F Function(Exception e) onUnknownError,
}) async {
  try {
    final T result = await call();
    return right(result);
  } on DioException catch (e) {
    // logger.e("BaseService.safeCall: ", e);
    switch (e.type) {
      case DioExceptionType.connectionTimeout:
        return left(onNetworkError());
      case DioExceptionType.sendTimeout:
        return left(onNetworkError());
      case DioExceptionType.receiveTimeout:
        return left(onNetworkError());
      case DioExceptionType.badResponse:
        return left(onResponseError(e.response));
      case DioExceptionType.cancel:
        return left(onNetworkError());
      case DioExceptionType.unknown:
        if (e.error is SocketException) {
          return left(onNetworkError());
        }
        return left(onUnknownError(e));
      case DioExceptionType.badCertificate:
        return left(onNetworkError());
      case DioExceptionType.connectionError:
        return left(onNetworkError());
      case DioExceptionType.unknown:
        return left(onNetworkError());
    }
  }
}