tryCatch<L, R> function

TaskEither<L, R> tryCatch<L, R>(
  1. Lazy<FutureOr<R>> task,
  2. L onError(
    1. dynamic err,
    2. StackTrace stackTrace
    )
)

Runs the given task, and returns the result as an Right. If it throws an error, the the error is passed to onError, which determines the Left value.

expect(
  await tryCatch(() => 'hello', (err, stack) => 'fail')(),
  E.right('hello'),
);
expect(
  await tryCatch(() => throw 'error', (err, stack) => 'fail')(),
  E.left('fail'),
);

Implementation

TaskEither<L, R> tryCatch<L, R>(
  Lazy<FutureOr<R>> task,
  L Function(dynamic err, StackTrace stackTrace) onError,
) =>
    TaskEither(Task(() => fromThrowable(
          task,
          onSuccess: either.right,
          onError: (err, stack) => either.left<L, R>(onError(err, stack)),
        )));