Error handling in workflow scripts

Managed by | Updated .

Workflow scripts can fail and errors must be handled appropriately. A common approach is to have a failing workflow script exit with a non-zero exit code so that Funnelback can detect the problem and fail the update (sending a notification email if it has been configured).

To achieve this in shell scripts, make sure you set the -e flag in the hashbang line: #!/bin/sh -e or #!/bin/bash -e. This will cause the shell to immediately exit with an error code if one the script commands fail.

To achieve this in Groovy script, throw an exception with a message of why the script cannot continue:

throw new Exception("Unable to connect to remote system");

Never use System.exit() as it will cause the JVM to shutdown. Depending on the context it may have bad consequences, such as shutting down the Jetty webserver if the script is being run by Jetty.

Was this artcle helpful?