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.
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.
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.