forked from arrow-kt/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Context
I'm using Arrow version 1.2.3.
I’ve created a simple helper for Arrow Core that introduces a Raise context. The helper is meant for use in automated tests, and is designed to throw AssertionError on failure. Since tests are expected not to fail, enabling tracing by default seems useful for debugging root causes — even with some performance cost.
Code
@ExperimentalTraceApi
inline fun <Failure, Success> failOnRaise(block: Raise<Failure>.() -> Success): Success {
val result = either {
traced(block) { trace, error ->
throw AssertionError("An operation raised $error\n${trace.stackTraceToString()}").apply {
for (suppressed in trace.suppressedExceptions())
addSuppressed(suppressed)
}
}
}
check(result is Either.Right) {
"Impossible situation: we throw an error when the passed block raises, but it still gave us a failed either: $result"
}
return result.value
}Used on test
failOnRaise {
raise(5)
}
Expected Behavior
Expected exception:
AssertionError: An operation raised 5
arrow.core.raise.Traced: // some trace output
Actual Behavior
Thrown exception:
java.lang.AssertionError: An operation raised 5
arrow.core.raise.Traced: kotlin.coroutines.cancellation.CancellationException should never get swallowed. Always re-throw it if captured. This swallows the exception of Arrow's Raise, and leads to unexpected behavior. When working with Arrow prefer Either.catch or arrow.core.raise.catch to automatically rethrow CancellationException.
at arrow.core.raise.DefaultRaise.raise(Fold.kt:270)
at opensavvy.prepared.compat.arrow.core.FailOnRaiseTest$1$3.invokeSuspend(FailOnRaiseTest.kt:21)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels