RaiseScope

interface RaiseScope<in Error>

A scope for raising typed errors without exceptions leaking to callers.

Create a new scope with DefaultRaiseScope, or use a builder like outcomeOf / faultOf. Inside the scope, call raise to short-circuit execution with an Error. The scope's fold / folding captures the raised error and maps it to an output value.

Parameters

Error

The type of error that can be raised within this scope.

See also

Inheritors

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
inline fun <Ok, Error> RaiseScope<Error>.catch(catch: (throwable: Throwable) -> Error = ::rethrow, block: RaiseScope<Error>.() -> Ok): Ok

Executes block inside the current RaiseScope, converting any thrown Throwable — including fatal kotlin.Error subclasses — to a raised Error via catch.

Link copied to clipboard
inline suspend fun <Ok, Error> RaiseScope<Error>.catching(catch: (throwable: Throwable) -> Error = ::rethrow, block: RaiseScope<Error>.() -> Ok): Ok

Executes block inside the current RaiseScope, converting any thrown Throwable — including fatal kotlin.Error subclasses — to a raised Error via catch.

Link copied to clipboard
abstract fun close()

Marks the scope as expired.

Link copied to clipboard
inline fun <Error> RaiseScope<Error>.ensure(condition: Boolean, raise: () -> Error)

Asserts condition is true, otherwise raises the Error produced by raise.

Link copied to clipboard
inline fun <Value, IsType : Any, Error> RaiseScope<Error>.ensureInstanceOf(check: Value, isType: KClass<IsType>, error: (Value) -> Error = { @Suppress(names = ["UNCHECKED_CAST"]) (it as Error) }): IsType

Returns check cast to IsType if it is an instance of that type, otherwise raises the Error produced by error.

Link copied to clipboard
inline fun <Ok, Error> RaiseScope<Error>.ensureNotNull(value: Ok?, raise: () -> Error): Ok & Any

Returns value if it is non-null, otherwise raises the Error produced by raise.

Link copied to clipboard
inline fun <In, Out, Error> RaiseScope<Error>.fold(block: (scope: RaiseScope<Error>) -> In, catch: (Exception) -> Out = ::rethrow, recover: (error: Error) -> Out, transform: (value: In) -> Out): Out

Executes block within this RaiseScope and maps the result using transform, recover, or catch.

Link copied to clipboard
inline suspend fun <In, Out, Error> RaiseScope<Error>.folding(block: suspend (scope: RaiseScope<Error>) -> In, catch: (Exception) -> Out = ::rethrow, recover: suspend (error: Error) -> Out, transform: suspend (value: In) -> Out): Out

Suspend variant of fold that propagates CancellationException for structured concurrency.

Link copied to clipboard

Type-hint helper — tells the compiler the Error type of this RaiseScope is Nothing. Use when type inference fails and no error can ever be raised.

Link copied to clipboard
inline fun <Error> RaiseScope<Error>.raise(error: () -> Error): Nothing

Evaluates error and delegates to shortCircuit, immediately short-circuiting the RaiseScope execution.

Link copied to clipboard

Type-hint helper — tells the compiler the Error type of this RaiseScope. Use when type inference fails and no value is available to infer from.

Type-hint helper — tells the compiler the Error type of this RaiseScope using a concrete type value. Use when type inference fails and a representative value is available.

Link copied to clipboard
abstract fun shortCircuit(error: Error): Nothing

Throws the given error, immediately short-circuiting the RaiseScope execution.