Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions fixed-failOnRaise.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@ExperimentalTraceApi
inline fun <Failure, Success> failOnRaise(block: Raise<Failure>.() -> Success): Success {
val result = either {
catch({
traced(block) { trace, error ->
throw AssertionError("An operation raised $error\n${trace.stackTraceToString()}").apply {
for (suppressed in trace.suppressedExceptions())
addSuppressed(suppressed)
}
}
}) { throwable ->
// Rethrow CancellationException and other fatal exceptions
throw throwable.nonFatalOrThrow()
}
}
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
}