diff --git a/fixed-failOnRaise.kt b/fixed-failOnRaise.kt new file mode 100644 index 00000000000..440979bc372 --- /dev/null +++ b/fixed-failOnRaise.kt @@ -0,0 +1,20 @@ +@ExperimentalTraceApi +inline fun failOnRaise(block: Raise.() -> 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 +}