chainTryCatchK<L, R, R2> function

Either<L, R2> Function(Either<L, R> value) chainTryCatchK<L, R, R2>(
  1. R2 f(
    1. R right
    ),
  2. L onError(
    1. dynamic error,
    2. StackTrace stack
    )
)

Runs the given function, and the result is wrapped in a Right. If it raises an error, then the onError callback determines the Left value.

The returned function accepts an Either, and the transformation is only run if is a Right value.

final catcher = chainTryCatchK(
  (int i) => i > 5 ? i : throw 'error',
  (err, stack) => 'number too small',
);

expect(
  right(10).chain(catcher),
  right(10),
);
expect(
  right(3).chain(catcher),
  left('number too small'),
);

Implementation

Either<L, R2> Function(Either<L, R> value) chainTryCatchK<L, R, R2>(
  R2 Function(R right) f,
  L Function(dynamic error, StackTrace stack) onError,
) =>
    flatMap(tryCatchK(f, onError));