-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Alejandro Serrano <trupill@gmail.com> Co-authored-by: franciscodr <francisco.d@47deg.com> Co-authored-by: Youssef Shoaib <canonballt@gmail.com>
- Loading branch information
1 parent
4a869af
commit 0a86ec2
Showing
11 changed files
with
227 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/raise/Trace.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
@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 | ||
|
||
@RequiresOptIn("This API is experimental, and may change in the future.") | ||
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.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.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<Throwable> = | ||
exception.suppressedExceptions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/raise/TraceSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package arrow.core.raise | ||
|
||
import arrow.core.right | ||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.int | ||
import io.kotest.property.arbitrary.string | ||
import io.kotest.property.checkAll | ||
import kotlinx.coroutines.CompletableDeferred | ||
|
||
@OptIn(ExperimentalTraceApi::class) | ||
class TraceSpec : StringSpec({ | ||
"trace is empty when no errors" { | ||
checkAll(Arb.int()) { i -> | ||
either<Nothing, Int> { | ||
traced({ i }) { _,_ -> unreachable() } | ||
} shouldBe i.right() | ||
} | ||
} | ||
|
||
"trace is empty with exception" { | ||
checkAll(Arb.string()) { msg -> | ||
val error = RuntimeException(msg) | ||
shouldThrow<RuntimeException> { | ||
either<Nothing, Int> { | ||
traced({ throw error }) { _,_ -> unreachable() } | ||
} | ||
}.message shouldBe msg | ||
} | ||
} | ||
|
||
"nested tracing - identity" { | ||
val inner = CompletableDeferred<String>() | ||
ior(String::plus) { | ||
traced({ | ||
traced({ raise("") }) { traced, _ -> | ||
inner.complete(traced.stackTraceToString()) | ||
} | ||
}) { traced, _ -> | ||
inner.await() shouldBe traced.stackTraceToString() | ||
} | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.