All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.kase.Failure.kt Maven / Gradle / Ivy

There is a newer version: 3.0.13
Show newest version
@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"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy