[minor] Add a small diagnostics handling utility fun

This commit is contained in:
Ilya Chernikov
2019-07-22 14:11:58 +02:00
parent 0072f8c265
commit 65e6d3b0ff
@@ -10,6 +10,7 @@ package kotlin.script.experimental.api
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.PrintStream
import java.lang.RuntimeException
/**
* The single script diagnostic report
@@ -220,3 +221,14 @@ inline fun <R> ResultWithDiagnostics<R>.valueOr(body: (ResultWithDiagnostics.Fai
is ResultWithDiagnostics.Success<R> -> value
is ResultWithDiagnostics.Failure -> body(this)
}
/**
* Extracts the result value from the receiver wrapper or throw RuntimeException with diagnostics
*/
fun <R> ResultWithDiagnostics<R>.valueOrThrow(): R = valueOr {
throw RuntimeException(
reports.joinToString("\n") { it.exception?.toString() ?: it.message },
reports.find { it.exception != null }?.exception
)
}