recoverCatching method

Result<T> recoverCatching({
  1. required T transform(
    1. Failure failure
    ),
  2. bool test(
    1. Object error
    )?,
})

Returns the encapsulated result of the given transform function applied to the encapsulated error and stackTrace in Failure if this instance represents Result.isFailure or the original encapsulated value if it is Result.isSuccess.

This function catches any error thrown by transform function, then test is called with the error value, if test is true, the error and the StackTrace are encapsulating it as a failure, otherwise, the error is thrown

If test is omitted, it defaults to a function that always returns true.

The transform function is called inside runCatching.

See recover for an alternative that rethrows error.

Implementation

Result<T> recoverCatching({
  required T Function(Failure failure) transform,
  bool Function(Object error)? test,
}) {
  final failure = failureOrNull();
  if (failure == null) {
    return this;
  }
  return runCatching(() => transform(failure), test: test);
}