ClerkResult
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.
Failure policy
A function that returns ClerkResult reports every failure through Failure rather than throwing:
Server and transport errors keep the API error, HTTP status code, or throwable.
Invalid arguments (for example a DSL builder block that leaves out a required value) and resources missing data the call needs are reported as Failure.ErrorType.API failures with a ClerkErrorResponse that carries a descriptive error code and message.
Unexpected exceptions from the platform (Credential Manager, Play Integrity, I/O) are reported as Failure.ErrorType.UNKNOWN failures with the exception in Failure.throwable.
Flows that document a specific exception type for a failure, such as a dismissed or superseded browser flow, report it as Failure.ErrorType.UNKNOWN with that exception in Failure.throwable.
Two things still propagate as exceptions: coroutine cancellation (kotlin.coroutines.cancellation.CancellationException), so structured concurrency keeps working, and calling the SDK before Clerk.initialize has configured it, which throws IllegalStateException because it is a setup error rather than a runtime failure.
Use map, mapError, flatMap and fold to transform results without unpacking them.
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
A unified failure type that contains all necessary error information.
Functions
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.
Returns a ClerkResult.Success holding transform applied to the value of a ClerkResult.Success, keeping its tags, or returns the original ClerkResult.Failure unchanged.
Returns a ClerkResult.Failure whose error is transform applied to the original error, keeping the throwable, status code, error type and tags, or returns the original ClerkResult.Success unchanged. transform is not called when the failure carries no error.
Performs the given action on the encapsulated ClerkResult.Failure if this instance represents failure. Returns the original ClerkApiResult unchanged.
Performs the given action on the encapsulated failure if this instance represents a failure with the specified error type. Returns the original ClerkApiResult unchanged.
If ClerkResult.Success, returns the underlying T value. Otherwise, returns the result of the defaultValue function.
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).
If ClerkResult.Success, returns the underlying T value. Otherwise, returns null.
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.
Transforms an ClerkResult into a C value.