catchStreamError<L, R> static method

Stream<Either<L, R>> catchStreamError<L, R>(
  1. ErrorMapper<L> errorMapper,
  2. Stream<R> stream
)

Transforms data events to Rights and error events to Lefts.

When the source stream emits a data event, the result stream will emit a Right wrapping that data event.

When the source stream emits a error event, calling errorMapper with that error and the result stream will emits a Left wrapping the result.

The done events will be forwarded.

Example

final Stream<int> s = Stream.fromIterable([1, 2, 3, 4]);
final Stream<Either<Object, int>> eitherStream = Either.catchStreamError((e, s) => e, s);

eitherStream.listen(print); // prints Either.Right(1), Either.Right(2),
                            // Either.Right(3), Either.Right(4),
final Stream<int> s = Stream.error(Exception());
final Stream<Either<Object, int>> eitherStream = Either.catchStreamError((e, s) => e, s);

eitherStream.listen(print); // prints Either.Left(Exception)

Implementation

static Stream<Either<L, R>> catchStreamError<L, R>(
  ErrorMapper<L> errorMapper,
  Stream<R> stream,
) =>
    stream.transform(
      StreamTransformer<R, Either<L, R>>.fromHandlers(
        handleData: (data, sink) => sink.add(Either.right(data)),
        handleError: (e, s, sink) =>
            sink.add(Either.left(errorMapper(e.throwIfFatal(), s))),
      ),
    );