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

commonMain.com.bkahlert.kommons.exceptions.kt Maven / Gradle / Ivy

Go to download

Kommons Core is a Kotlin Multiplatform Library that offers shared features for all Kommons modules.

There is a newer version: 2.8.0
Show newest version
package com.bkahlert.kommons

/**
 * The root cause of this [Throwable], that is,
 * the throwable that was thrown the first.
 */
public val Throwable.rootCause: Throwable
    get() {
        var rootCause: Throwable = this
        while (rootCause.cause != null && rootCause.cause !== rootCause) {
            rootCause = rootCause.cause ?: error("Must not happen.")
        }
        return rootCause
    }

/**
 * The causes of this [Throwable], that is,
 * a list containing the [Throwable.cause], the cause of the cause, ...
 */
public val Throwable.causes: List
    get() = when (val cause = cause) {
        null -> emptyList()
        else -> buildList {
            add(cause)
            addAll(cause.causes)
        }
    }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy