faultOf

inline suspend fun <Error> faultOf(catch: (Exception) -> Fault<Error> = ::rethrow, scope: RaiseScope<Error> = DefaultRaiseScope(), block: suspend RaiseScope<Error>.() -> Unit): Fault<Error>

Suspend variant of fault builder.

Runs block inside a RaiseScope and returns a Fault. kotlinx.coroutines.CancellationException is correctly propagated for structured concurrency.

The success value is always Unit; use raise to short-circuit with an Error.

// Fault<String> — raises an error
val f: Fault<String> = faultOf { raise { "something went wrong" } }

// Fault<Exception> — exception mapped via catch
val g: Fault<Exception> = faultOf(catch = ::Failure) { throw IllegalStateException("oops") }

Parameters

catch

Maps a thrown Exception to a Fault. Rethrows by default — override to suppress exceptions.

block

The suspend block to execute within the scope.

See also