tryCatchK<A, L, R> function

Either<L, R> Function(A value) tryCatchK<A, L, R>(
  1. R f(
    1. A value
    ),
  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 function accepts an externally passed value.

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

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

Implementation

Either<L, R> Function(A value) tryCatchK<A, L, R>(
  R Function(A value) f,
  L Function(dynamic error, StackTrace stack) onError,
) =>
    (value) => tryCatch(() => f(value), onError);