handleErrorWith<C> method

Either<C, R> handleErrorWith<C>(
  1. Either<C, R> f(
    1. L value
    )
)

Handle any error, potentially recovering from it, by mapping it to an Either value.

Applies the given function f if this is a Left, otherwise returns this if this is a Right. This is like flatMap for the exception.

Example

Right<int, int>(12).handleErrorWith((v) => (v + 1).right<String>());   // Right(12)
Right<int, int>(12).handleErrorWith((v) => (v + 1).toString().left()); // Right(12)
Left<int, int>(12).handleErrorWith((v) => (v + 1).right<String>());    // Right(13)
Left<int, int>(12).handleErrorWith((v) => (v + 1).toString().left());  // Left('13')

Implementation

Either<C, R> handleErrorWith<C>(Either<C, R> Function(L value) f) =>
    _foldInternal(
      ifLeft: f,
      ifRight: (v) => v.right<C>(),
    );