commonMain.kase.Failure.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kase-core Show documentation
Show all versions of kase-core Show documentation
A kotlin multiplatform library to present common UI states
@file:JsExport
@file:Suppress("NON_EXPORTABLE_TYPE")
package kase
import kase.internal.AbstractPossible
import kotlinx.JsExport
data class Failure(
val cause: Throwable,
val message: String = cause.message ?: DEFAULT_MESSAGE,
override val data: D? = null
) : AbstractPossible(), LazyState, Result, ExecutorState {
override val asPending: Nothing? = null
override val asLoading: Nothing? = null
override val asExecuting: Nothing? = null
override val asSuccess: Nothing? = null
override val asFailure: Failure = this
override fun map(transform: (D) -> R): Failure = mapToState(transform)
override fun catch(resolver: (Throwable) -> @UnsafeVariance D): Result = catchToState(resolver) as Result
override fun andCatch(resolver: (Throwable) -> Result<@UnsafeVariance D>): Result = resolver(cause)
override fun exists(): Boolean = false
override fun valueOr(default: @UnsafeVariance D): D = default
override fun valueOrThrow(): D = throw cause
override fun valueOrThrow(exp: Throwable): D {
exp.addSuppressed(cause)
throw exp
}
override fun valueOrNull(): D? = data
override fun valueOrThrow(msg: String): D = valueOrThrow(RuntimeException(msg, cause))
override fun onSuccess(callback: (D) -> Unit) = this
override fun onFailure(callback: (Throwable) -> Unit): Failure {
callback(cause)
return this
}
companion object {
val DEFAULT_MESSAGE = "Unknown error"
}
}