Understanding CLI Scripting Errors
When an error is generated, an exception is thrown. The exception is generally an object that contains the following members:
-
code
- a numeric code associated with the error -
message
- a human-readable message associated with the error
Exceptions can be caught and handled, or they may be thrown out of the script environment. If a script environment has an uncaught exception, the CLI will display the details. For example:
hostname:> script run('not a cmd')
error: uncaught error exception (code EAKSH_BADCMD) in script: invalid command
"not a cmd" (encountered while attempting to run command "not a cmd")
You could see more details about the exception by catching it and dumping it out:
hostname:> script try { run('not a cmd') } catch (err) { dump(err); }
{
toString: <function>,
code: 10004,
message: 'invalid command "not a cmd" (encountered while attempting to
run command "not a cmd")'
}
This also allows you to have rich error handling; for example:
#!/usr/bin/ksh -p ssh -T root@hostname <<EOF script try { run('shares select default select $1'); } catch (err) { if (err.code == EAKSH_ENTITY_BADSELECT) { printf('error: "$1" is not a share in the ' + 'default project\n'); exit(1); } throw (err); } printf('"default/$1": compression is %s\n', get('compression')); exit(0); EOF
If this script is named share.ksh
and run with an invalid share
name, a rich error message will be generated:
$ ksh ./share.ksh bogus
error: "bogus" is not a share in the default project