catchFutureError<L, R> static method
Evaluates the specified block
and wrap the result in a Right.
If an error is thrown or block
returns a future that completes with an error,
calling errorMapper
with that error and wrap the result in a Left.
Example
// Result: Left(Exception())
await Either.catchFutureError<Object, String>(
(e, s) => e,
() => throw Exception())
);
// Result: Left(Exception())
await Either.catchFutureError<Object, String>(
(e, s) => e,
() async => throw Exception())
);
// Result: Right('hoc081098')
await Either.catchFutureError<Object, String>(
(e, s) => e,
() => Future.value('hoc081098'))
);
// Result: Right('hoc081098')
await Either.catchFutureError<Object, String>(
(e, s) => e,
() async => await Future.value('hoc081098'))
);
Implementation
static Future<Either<L, R>> catchFutureError<L, R>(
ErrorMapper<L> errorMapper,
FutureOr<R> Function() block,
) =>
Future.sync(block)
.then((value) => Either<L, R>.right(value))
.onError<Object>(
(e, s) => Either.left(errorMapper(e.throwIfFatal(), s)));