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

fuookami.ospf.kotlin.utils.error.Error.kt Maven / Gradle / Ivy

There is a newer version: 1.0.29
Show newest version
package fuookami.ospf.kotlin.utils.error

sealed class Error {
    companion object {
        operator fun invoke(code: ErrorCode, message: String): Err {
            return Err(code, message)
        }
    }

    abstract val code: ErrorCode
    abstract val message: String
    open val value: Any? = null

    val withValue get() = value != null

    override fun toString(): String {
        return if (value != null) {
            "$code: $message($value)"
        } else {
            "$code: $message"
        }
    }
}

open class Err(
    override val code: ErrorCode,
    override val message: String
) : Error() {
    companion object {
        operator fun invoke(code: ErrorCode, message: String? = null): Err {
            return if (message == null) {
                Err(code, code.toString())
            } else {
                Err(code, message)
            }
        }
    }
}

open class ExErr(
    override val code: ErrorCode,
    override val message: String,
    override val value: T
) : Error() {
    constructor(code: ErrorCode, value: T) : this(code, code.toString(), value)
}

data class ApplicationException(
    val error: Error
) : Throwable() {
    override val message: String by error::message

    override fun toString(): String {
        return "$error"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy