Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw on errors ? #23

Open
martinbonnin opened this issue Aug 5, 2021 · 2 comments
Open

Throw on errors ? #23

martinbonnin opened this issue Aug 5, 2021 · 2 comments

Comments

@martinbonnin
Copy link
Contributor

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}"()
    }
@jakubriegel
Copy link
Owner

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

@martinbonnin
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants