DataResult

sealed class DataResult<out T>

Container for a database operation result, which can end in success or failure. Replaces the exception mechanism, forcing explicit handling of both cases.

Parameters

T

Data type in case of success.

Inheritors

Constructors

Link copied to clipboard
protected constructor()

Types

Link copied to clipboard
data class Failure(val error: DatabaseException) : DataResult<Nothing>

Represents an error during operation.

Link copied to clipboard
data class Success<out T>(val value: T) : DataResult<T>

Represents successful operation execution.

Functions

Link copied to clipboard
fun <R, T : R> DataResult<T>.getOrElse(onFailure: (Throwable) -> R): R

Returns the value if the result is Success, or computes a default value from the error if it is Failure.

Link copied to clipboard

Returns the value if the result is Success, or throws an exception if it is Failure.

Link copied to clipboard
inline fun <T, R> DataResult<T>.map(transform: (T) -> R): DataResult<R>

Transforms the value inside DataResult.Success, leaving DataResult.Failure unchanged.

Link copied to clipboard

Executes an action if the result is Failure, without modifying the original error.

Link copied to clipboard
fun <T> DataResult<T>.onSuccess(action: (T) -> Unit): DataResult<T>

Executes an action if the result is Success, without modifying the original value.