kotlin-shell
kotlin-shell copied to clipboard
Throw on errors ?
Is it possible to automatically throw on errors? Similar to set -e
:
shell {
throwOnNonZeroStatus() // or simply throwOnError() ?
// will fail the script if the zip file is not present
"unzip ${zipFile.absolutePath}"()
}
Kotlin Shell doesn't have such functionality built in. You could achieve something similar with checking if stderr
is not empty after project is executed
I have the following extension function that works:
private fun Process.throwOnError() {
if (this.pcb.exitCode != 0) {
throw Exception("${this.name} failed with status ${this.pcb.exitCode}")
}
}
It has to be called on each call though:
"unzip ${zipFile.absolutePath}"().throwOnError()
"unzip ${zipFile2.absolutePath}"().throwOnError()