tryRecoverOf

inline suspend fun <T> KotlinResult<T>.tryRecoverOf(failure: (Throwable) -> T): KotlinResult<T>

Attempts to recover from a failure by transforming the encapsulated Throwable into a success value, wrapping failure in resultOf.

Unlike Result.recover, exceptions from failure are caught and re-encapsulated. Unlike Result.recoverCatching, CancellationException is re-thrown when the coroutine is no longer active.

Successes pass through unchanged.

resultOf { 4 }                                       // Result.success(4)
.tryRecoverOf { Unit } // No change — Result.success(4)
.andThenOf { throw FileNotFoundException("test") } // Result.failure(FileNotFoundException("test"))
.tryRecoverOf { 7 } // Result.success(7)

See also