// Generic DatError
val result: DatResult<ChallengeData> = attestationClient.requestChallenge(request)
// Narrow to AttestationError only
val result: DatResult<ChallengeData, AttestationError> = attestationClient.requestChallenge(request)
// Type-safe error handling with 2 parameters
result
.onSuccess { data -> println("Got: $data") }
.onFailure { error, cause ->
when (error) {
AttestationError.TOKEN_NOT_CONFIGURED -> showConfigError()
AttestationError.NETWORK_ERROR -> retryRequest()
}
}
// Standard exception handling with 1 parameter
result.onFailure { exception -> Log.e(TAG, "Failed", exception) }
value class DatResult<T, E : DatError> : Serializable
isFailure
: Boolean
[Get] |
Signature
val isFailure: Boolean |
isSuccess
: Boolean
[Get] |
Signature
val isSuccess: Boolean |
errorOrNull
()
|
Returns the error if this is a failure, or null if success. The error type E is enforced by the type system.
Note: The cast to E is safe by design because DatResult can only be constructed with errors of type E, and Result is immutable. This is enforced at construction time through the type system.
Signature
fun errorOrNull(): E? Returns DatResult? |
exceptionOrNull
()
|
Signature
fun exceptionOrNull(): Throwable? Returns Throwable? |
fold
(
onSuccess
, onFailure
)
|
Folds this result into a value by applying onSuccess or onFailure. Unlike standard Result.fold, onFailure receives the typed DatError directly.
Note: The cast to E is safe by design because DatResult can only be constructed with errors of type E, and Result is immutable.
Signature
inline fun <R> fold(onSuccess: (T) -> R, onFailure: (error: E, cause: Throwable?) -> R): R Parameters onSuccess: Function1onFailure: Function2Returns DatResult |
getOrDefault
(
defaultValue
)
| |
getOrElse
(
onFailure
)
|
Signature
inline fun getOrElse(onFailure: (exception: Throwable) -> T): T Parameters onFailure: Function1Returns DatResult |
getOrNull
()
|
Signature
fun getOrNull(): T? Returns DatResult? |
getOrThrow
()
|
Signature
fun getOrThrow(): T Returns DatResult |
map
(
transform
)
|
Signature
inline fun <R> map(transform: (value: T) -> R): DatResult<R, E> Parameters transform: Function1Returns DatResult |
mapCatching
(
transform
)
|
Signature
inline fun <R> mapCatching(transform: (value: T) -> R): Result<R> Parameters transform: Function1Returns Result<R> |
onFailure
(
action
)
|
Performs the given action if this is a failure, providing direct access to the typed DatError. Use this version with 2 parameters for type-safe error handling.
Usage: result.onFailure { error, cause -> ... }
Note: The cast to E is safe by design because DatResult can only be constructed with errors of type E, and Result is immutable.
Signature
inline fun onFailure(action: (error: E, cause: Throwable?) -> Unit): DatResult<T, E> Parameters action: Function2Returns DatResult |
onFailure
(
action
)
|
Performs the given action if this is a failure, receiving the exception. Use this version with 1 parameter for standard exception handling.
Usage: result.onFailure { exception -> ... }
Signature
inline fun onFailure(action: (exception: Throwable) -> Unit): DatResult<T, E> Parameters action: Function1Returns DatResult |
onSpecificError
(
action
)
|
Performs the given action if this is a failure with a specific error type.
Usage: result.onSpecificError<SpecificError> { error, cause -> ... }
Signature
inline fun <F : DatError> onSpecificError(action: (error: F, cause: Throwable?) -> Unit): DatResult<T, E> Parameters action: Function2Returns DatResult |
onSuccess
(
action
)
|
Signature
inline fun onSuccess(action: (value: T) -> Unit): DatResult<T, E> Parameters action: Function1Returns DatResult |
recover
(
transform
)
|
Signature
inline fun recover(transform: (exception: Throwable) -> T): DatResult<T, E> Parameters transform: Function1Returns DatResult |
recoverCatching
(
transform
)
|
Signature
inline fun recoverCatching(transform: (exception: Throwable) -> T): Result<T> Parameters transform: Function1Returns Result<T> |
toResult
()
|
Unwraps to the underlying standard Result for stdlib interop.
Signature
fun toResult(): Result<T> Returns Result<T> |
validateErrorType
()
|
Validates error type at runtime using reified generics. Use this when you need extra type safety beyond compile-time guarantees (e.g., when deserializing, crossing API boundaries, or dealing with type-erased results).
Returns null if the result contains an error that doesn't match the expected type E.
Example:
val untypedResult: DatResult<String, DatError> = getResult()
val typedResult: DatResult<String, AttestationError>? = untypedResult.validateErrorType()
if (typedResult != null) {
// Safe to use as AttestationError
typedResult.onFailure { error, _ ->
when (error) {
AttestationError.TOKEN_NOT_CONFIGURED -> ...
}
}
}
Signature
inline fun <T, E : DatError> DatResult<T, *>.validateErrorType(): DatResult<T, E>? |
failure
(
exception
)
|
Creates a failed DatResult from a DatException.
The error type E is inferred from the exception's error property.
Signature
fun <T, E : DatError> failure(exception: DatException): DatResult<T, E> Parameters exception: DatExceptionReturns DatResult |
failure
(
error
, cause
)
|
Creates a failed DatResult from a DatError, wrapping it in DatException.
This is a convenience method to reduce boilerplate when you have a DatError and want to create a Result failure without manually constructing the exception. The error type E is inferred from the error parameter.
Signature
fun <T, E : DatError> failure(error: E, cause: Throwable? = null): DatResult<T, E> Parameters error: DatResult.Companion
The DatError to wrap (type E is inferred from this parameter)
cause: Throwable?
Optional underlying cause
Returns DatResult |
success
(
value
)
|
Creates a successful DatResult with the given value.
The error type E must be specified explicitly or inferred from context:
val result: DatResult<String, AttestationError> = DatResult.success("hello")
Signature
fun <T, E : DatError> success(value: T): DatResult<T, E> Parameters value: DatResult.CompanionReturns DatResult |
wrap
(
result
)
|
Wraps an existing standard Result as a DatResult.
Warning: Assumes the Result's failure is a DatException. If the Result contains a different exception type, calling DatResult-specific methods will throw ClassCastException. The error type E must be specified explicitly or inferred from context.
Signature
fun <T, E : DatError> wrap(result: Result<T>): DatResult<T, E> Parameters result: Result<T>Returns DatResult |