tryEither<T> function

Result<T> tryEither<T>(
  1. T block(
    1. BindEither bind
    )
)

Wraps block function into try..catch and returns Right with the result. In case of any Exception returns Left with the exception.

It doesn't catch Errors as it usually means a bug in the code.

Implementation

Result<T> tryEither<T>(T Function(BindEither bind) block) {
  A bind<A>(Result<A> either) => either.fold((e) => throw e, identity);

  try {
    return Either.right(block(bind));
  } on Exception catch (error) {
    return Either.left(error);
  }
}