try Recover
inline fun <Ok : Any, ErrorIn : Any, ErrorOut : Any> Outcome<Ok, ErrorIn>.tryRecover(catch: (throwable: Throwable) -> Outcome<Ok, ErrorOut> = ::rethrow, failure: RaiseScope<ErrorOut>.(ErrorIn) -> Ok): Outcome<Ok, ErrorOut>
Transform failure into success.
Success ->
returns
the original caller.Failure ->wraps&
returns
the result of onFailure transformation.If onFailure throws an exception, it will be caught & wrapped by outcomeOf.
This is the outcomeOf alternative to Outcome.recover.
outcomeOf { 4 } // Outcome.success(4)
.tryRecover { Unit } // No Change - Outcome.success(4)
.andThen { throw FileNotFoundException("test") } // Outcome.failure(FileNotFoundException("test"))
.tryRecover { 7 } // Outcome.success(7)
Content copied to clipboard