JDK-8087292 : nashorn should have a "fail-fast" option for scripting, analog to bash "set -e"
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 8u45
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_8
  • CPU: x86
  • Submitted: 2015-06-11
  • Updated: 2016-01-14
  • Resolved: 2015-09-03
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u72Fixed 9 b81Fixed
Related Reports
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
When I write a script, I want it to fail as soon as one of the commands doesn't return normally (i.e. has non-zero exit code). This functionality is available in other shells, e.g. in bash it's "set -e".

JUSTIFICATION :
This makes scripts safer, by aborting them as soon as a command fails & preventing other commands from causing more harm. A simple example:
cp importantFile anotherFile
rm importantFile
Assuming the first command fails & there's no fail-fast, "importantFile" will be lost.

It also greatly helps in debugging: with this fail-fast behavior, Nashorn could tell exactly which command failed. Without it, a script may run fine for some time after the failed command, and then suddenly crash on a seemingly unrelated command. It may be very hard at that point to find out what went wrong.

Currently, the alternatives are:
- to explicitly check $EXIT after each command. This results in a lot of boilerplate code.
- to create a helper function to wrap all commands. This implies that you have to run all commands by invoking a function, instead of being able to use the much cleaner backticks syntax.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I would like a way to specify fail-fast behavior for Nashorn scripts. It should be possible to specify this in the script itself, so I can just run:
jjs -scripting myNashornScript.js

For example, by introducing a new scripting mode function to set certain options:
set(ABORT_ON_CMD_FAILURE, true);


Comments
User can set "throwOnError" property of $EXEC function to be "true" or "false". This property is checked when $EXEC gets non-zero exit code from spawned process. If $EXEC.throwOnError is true, ECMAScript RangeError is thrown.
03-09-2015

Perhaps "throw exception" on non-zero exit code may be added (as an option) - exception forces script to stop automatically (assuming there is no blanket catch-and-do-nothing around!).
03-09-2015