guardStream<R> method

Stream<Either<Failure, R>> guardStream<R>(
  1. Stream<R> streamCallback()
)

Protects the provided function from throwing an unhandled exception

The left side of the returned Either will be a FirestoreFailure if any errors are triggered by the streamCallback

See also: guard for Future types

Implementation

Stream<Either<Failure, R>> guardStream<R>(
  Stream<R> Function() streamCallback,
) async* {
  try {
    await for (final result in streamCallback()) {
      yield Right(result);
    }
  } catch (e, stack) {
    if (e is FormatException) {
      // ignore: avoid_print
      print('Format Exception: ${e.message}');
    }

    yield Left(
      FirestoreFailure(
        error: e,
        stackTrace: Chain.forTrace(stack),
      ),
    );
  }
}