recover method

Result<T> recover(
  1. Result<T> onFailure(
    1. Failure failure
    )
)

Recovers from an operational failure by mapping the non-generic Failure instance into a new alternative Result.

Implementation

Result<T> recover(Result<T> Function(Failure failure) onFailure) {
  final current = this;

  // Check if the runtime implementation represents a failure state
  if (current.runtimeType.toString().contains('Failure')) {
    final dynamic internal = current;
    try {
      // Attempt direct casting if the object implements Failure directly
      return onFailure(internal as Failure);
    } catch (_) {
      // Fallback: extract the underlying failure instance if it is wrapped inside a private class
      return onFailure(internal.failure as Failure);
    }
  }

  // Bypass recovery and return the original success state untouched
  return this;
}