kotlin-shell icon indicating copy to clipboard operation
kotlin-shell copied to clipboard

Throw on errors ?

Open martinbonnin opened this issue 3 years ago • 2 comments

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}"()
    }

martinbonnin avatar Aug 05 '21 11:08 martinbonnin

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

jakubriegel avatar Aug 08 '21 12:08 jakubriegel

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()

martinbonnin avatar Aug 08 '21 18:08 martinbonnin