outcome Of
inline fun <Ok : Any, Error : Any> outcomeOf(catch: (throwable: Throwable) -> Outcome<Ok, Error> = ::rethrow, block: RaiseScope<Error>.() -> Ok): Outcome<Ok, Error>
Context runner that encapsulates the Ok result of block as an Outcome.Success, and any raised or caught errors as an Outcome.Failure.
Note: catch will rethrow by default. This is because the consumer needs to manually override the parameter and map it to an Outcome (if desired). Assigning it to Failure directly will only force Error to be interpreted as Throwable by the RaiseScope, which may interfere with the intended Error type!
// Outcome<Unit, Throwable>
outcomeOf(::Failure) { // this: RaiseScope<Throwable> -> ... }
// Outcome<Int, String>
outcomeOf { // this: RaiseScope<String> ->
raise { "error" }
return 3
}
// Outcome<String, NullPointerException>
outcomeOf { // this: RaiseScope<NullPointerException> ->
catch({ it }) { throw NullPointerException() }
}
Content copied to clipboard
Parameters
catch
Map thrown exceptions to an Outcome. (Throws by default).
block
The code to execute.