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

org.jetbrains.kotlinx.jupyter.exceptions.CompositeReplException.kt Maven / Gradle / Ivy

Go to download

Implementation of REPL compiler and preprocessor for Jupyter dialect of Kotlin (IDE-compatible)

There is a newer version: 0.12.0-333
Show newest version
package org.jetbrains.kotlinx.jupyter.exceptions

import java.io.PrintWriter

class CompositeReplException(
    private val exceptions: Collection,
    libraryProblemPart: LibraryProblemPart?,
) : ReplException(
        "CompositeException${libraryProblemPart?.message?.let { " in $it" }.orEmpty()}: ${exceptions.size} exceptions occurred.",
    ) {
    override fun printStackTrace() {
        printStackTrace(System.err)
    }

    override fun printStackTrace(pw: PrintWriter) {
        synchronized(pw) {
            pw.println(message)
            pw.println("-------------------------------")
            exceptions.forEachIndexed { index, throwable ->
                pw.println("Exception $index:")
                throwable.printStackTrace(pw)
                pw.println("-------------------------------")
            }
        }
    }

    /**
     * Returns true if any of the exceptions in the composite exception match the specified predicate.
     */
    fun contains(predicate: (Throwable) -> Boolean): Boolean {
        return exceptions.any(predicate)
    }

    /**
     * Returns a list of the causes of the exceptions in the composite exception.
     */
    fun getCauses(): List {
        return exceptions.flatMap { it.getCauses() }.distinct()
    }
}

/**
 * Returns a list of all the causes of a throwable.
 */
fun Throwable.getCauses(): List {
    return generateSequence(cause) { it.cause }.toList()
}

fun Collection.throwLibraryException(part: LibraryProblemPart) {
    when (size) {
        0 -> return
        1 -> throw ReplLibraryException(part = part, cause = first())
        else -> throw CompositeReplException(this, part)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy