tryCatch<L, R> function
TaskEither<L, R>
tryCatch<L, R>(
- Lazy<
FutureOr< task,R> > - L onError(
- dynamic err,
- 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)),
)));