guard<ValueT> static method

Future<Result<ValueT, Failure>> guard<ValueT>(
  1. Future<ValueT> run()
)

Transforms exceptions into Failure.other.

This is useful when

  • expected error type is Failure.
  • you don't need to handle specific error cases.

Implementation

static Future<Result<ValueT, Failure>> guard<ValueT>(
  Future<ValueT> Function() run,
) async {
  try {
    return Result.success(await run());
  } on Failure catch (e) {
    return Result.failure(e);
  } catch (e) {
    return const Result.failure(
      Failure.other,
    );
  }
}