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

commonMain.arrow.core.raise.Trace.kt Maven / Gradle / Ivy

The newest version!
@file:JvmMultifileClass
@file:JvmName("RaiseKt")
package arrow.core.raise

import kotlin.coroutines.cancellation.CancellationException
import kotlin.jvm.JvmInline
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName

@MustBeDocumented
@RequiresOptIn("This API is experimental, and may change in the future.")
@Retention(AnnotationRetention.BINARY)
public annotation class ExperimentalTraceApi

/** Tracing result. Allows to inspect the traces from where raise was called. */
@ExperimentalTraceApi
@JvmInline
public value class Trace(private val exception: CancellationException) {
  /**
   * Returns the stacktrace as a [String]
   *
   * Note, the first line in the stacktrace will be the `RaiseCancellationException`.
   * The users call to `raise` can found in the_second line of the stacktrace.
   */
  public fun stackTraceToString(): String = (exception.cause ?: exception).stackTraceToString()

  /**
   * Prints the stacktrace.
   *
   * Note, the first line in the stacktrace will be the `RaiseCancellationException`.
   * The users call to `raise` can found in the_second line of the stacktrace.
   */
  public fun printStackTrace(): Unit =
    (exception.cause ?: exception).printStackTrace()

  /**
   * Returns the suppressed exceptions that occurred during cancellation of the surrounding coroutines,
   *
   * For example when working with `Resource`, or `bracket`:
   * When consuming a `Resource` fails due to [Raise.raise] it results in `ExitCase.Cancelled`,
   * if the finalizer then results in a `Throwable` it will be added as a `suppressedException` to the [CancellationException].
   */
  public fun suppressedExceptions(): List =
    exception.cause?.suppressedExceptions.orEmpty() + exception.suppressedExceptions
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy