guard<T> static method

Result<T> guard<T>(
  1. T f()
)

Wraps a function call in a try-catch block. Returns a Success result if the function call succeeds. Returns a Failure result if the function call throws.

Implementation

static Result<T> guard<T>(T Function() f) {
  try {
    return Result.success(f());
  } catch (e, s) {
    return Result.failure(e, s);
  }
}