ClerkResult

sealed interface ClerkResult<out T : Any, out E : Any>

ClerkResult is a sealed interface used throughout the Clerk SDK to represent the result of an API call. It provides a type-safe and non-exceptional way to handle either a successful response with data or a failure with error details.

All consumer facing functions return ClerkResult values. This interface allows for pattern matching with Success and Failure cases using Kotlin's when expression.

Success contains the successfully retrieved data, while Failure wraps error information such as HTTP status codes, API errors, or unexpected failures. This design ensures predictable error handling and avoids checked exceptions.

Usage Example

// Example: Creating a sign-in
scope.launch {
val email = "user@example.com"
when (val result = ClerkApi().signIn(email)) {
is ClerkResult.Success -> {
val signIn = result.value.response
// Proceed with the sign-in flow based on available factors
val firstFactor = signIn.supportedFirstFactors.firstOrNull()
// Handle first factor preparation
}
is ClerkResult.Failure -> {
// Handle sign-in failure using error, code, or throwable
val errorMessage = result.error?.firstMessage() ?: "Unknown error"
}
}
}

Inheritors

Types

Link copied to clipboard
object Companion
Link copied to clipboard
class Failure<out E : Any>(val error: E?, val throwable: Throwable? = null, val code: Int? = null, val errorType: ClerkResult.Failure.ErrorType = ErrorType.UNKNOWN, tags: Map<KClass<*>, Any> = emptyMap()) : ClerkResult<Nothing, E>

A unified failure type that contains all necessary error information.

Link copied to clipboard
class Success<out T : Any>(val value: T, tags: Map<KClass<*>, Any>) : ClerkResult<T, Nothing>

A successful result with the data available in value.

Functions

Link copied to clipboard
inline fun <T : Any, R : Any, E : Any> ClerkResult<T, E>.flatMap(transform: (value: T) -> ClerkResult<R, E>): ClerkResult<R, E>

Returns a new ClerkResult by applying transform to the value of a ClerkResult.Success, or returns the original ClerkResult.Failure if this is a failure.

Link copied to clipboard
inline fun <T : Any, E : Any, C> ClerkResult<T, E>.fold(onSuccess: (value: T) -> C, onFailure: (failure: ClerkResult.Failure<E>) -> C): C

Transforms an ClerkResult into a C value.

Link copied to clipboard
inline fun <T : Any, E : Any> ClerkResult<T, E>.onFailure(action: (failure: ClerkResult.Failure<E>) -> Unit): ClerkResult<T, E>

Performs the given action on the encapsulated ClerkResult.Failure if this instance represents failure. Returns the original ClerkApiResult unchanged.

Link copied to clipboard
inline fun <T : Any, E : Any> ClerkResult<T, E>.onFailureType(errorType: ClerkResult.Failure.ErrorType, action: (failure: ClerkResult.Failure<E>) -> Unit): ClerkResult<T, E>

Performs the given action on the encapsulated failure if this instance represents a failure with the specified error type. Returns the original ClerkApiResult unchanged.

Link copied to clipboard
inline fun <T : Any, E : Any> ClerkResult<T, E>.onSuccess(action: (value: T) -> Unit): ClerkResult<T, E>

Performs the given action on the encapsulated value if this instance represents success. Returns the original ClerkApiResult unchanged.

Link copied to clipboard
inline fun <T : Any, E : Any> ClerkResult<T, E>.successOrElse(defaultValue: (failure: ClerkResult.Failure<E>) -> T): T

If ClerkResult.Success, returns the underlying T value. Otherwise, returns the result of the defaultValue function.

Link copied to clipboard
inline fun <T : Any, E : Any> ClerkResult<T, E>.successOrNothing(body: (failure: ClerkResult.Failure<E>) -> Nothing): T

If ClerkResult.Success, returns the underlying T value. Otherwise, calls body with the failure, which can either throw an exception or return early (since this function is inline).

Link copied to clipboard
fun <T : Any, E : Any> ClerkResult<T, E>.successOrNull(): T?

If ClerkResult.Success, returns the underlying T value. Otherwise, returns null.

Link copied to clipboard
inline suspend fun <T : Any, R : Any, E : Any> ClerkResult<T, E>.suspendingFlatMap(transform: suspend (value: T) -> ClerkResult<R, E>): ClerkResult<R, E>

Returns a new ClerkResult by applying transform to the value of a ClerkResult.Success, or returns the original ClerkResult.Failure if this is a failure.

Link copied to clipboard
inline suspend fun <T : Any, E : Any, C> ClerkResult<T, E>.suspendingFold(noinline onSuccess: suspend (value: T) -> C, noinline onFailure: (failure: ClerkResult.Failure<E>) -> C): C

Transforms an ClerkResult into a C value.