diff --git a/src/main/java/org/cobbzilla/util/system/CommandShell.java b/src/main/java/org/cobbzilla/util/system/CommandShell.java index e07439b..b9e14a8 100644 --- a/src/main/java/org/cobbzilla/util/system/CommandShell.java +++ b/src/main/java/org/cobbzilla/util/system/CommandShell.java @@ -202,7 +202,7 @@ public class CommandShell { try { return executor.execute(commandLine); } catch (Exception e) { - return die("chmod: "+e, e); + throw new CommandShellException(commandLine.toString(), e); } } @@ -230,7 +230,7 @@ public class CommandShell { try { return executor.execute(command); } catch (Exception e) { - return die(cmd+": "+e, e); + throw new CommandShellException(command.toString(), e); } } @@ -250,7 +250,7 @@ public class CommandShell { try { return exec(command).getStdout().trim(); } catch (IOException e) { - return die("Error executing: "+command+": "+e, e); + throw new CommandShellException(command, e); } } @@ -277,7 +277,7 @@ public class CommandShell { return temp; } catch (Exception e) { - return die("tempScript("+contents+") failed: "+e, e); + throw new CommandShellException(contents, e); } } @@ -288,7 +288,7 @@ public class CommandShell { public static String execScript (String contents, Map env, List exitValues) { final CommandResult result = scriptResult(contents, env, null, exitValues); if (!result.isZeroExitStatus() && (exitValues == null || !exitValues.contains(result.getExitStatus()))) { - die("execScript: non-zero exit: "+result); + throw new CommandShellException(contents, result); } return result.getStdout(); } @@ -310,12 +310,12 @@ public class CommandShell { if (!empty(exitValues)) command.setExitValues(exitValues); return exec(command); } catch (Exception e) { - return die("Error executing: "+e); + throw new CommandShellException(contents, e); } } public static CommandResult okResult(CommandResult result) { - if (result == null || !result.isZeroExitStatus()) die("error: "+result); + if (result == null || !result.isZeroExitStatus()) throw new CommandShellException(result); return result; } diff --git a/src/main/java/org/cobbzilla/util/system/CommandShellException.java b/src/main/java/org/cobbzilla/util/system/CommandShellException.java new file mode 100644 index 0000000..abf2662 --- /dev/null +++ b/src/main/java/org/cobbzilla/util/system/CommandShellException.java @@ -0,0 +1,28 @@ +package org.cobbzilla.util.system; + +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +@ToString(of={"command", "result", "exception"}) +public class CommandShellException extends RuntimeException { + + @Getter @Setter private String command; + @Getter @Setter private CommandResult result; + @Getter @Setter private Exception exception; + + public CommandShellException (CommandResult result) { this.result = result; } + + public CommandShellException (Exception e) { this.exception = e; } + + public CommandShellException (String command, Exception e) { + this(e); + this.command = command; + } + + public CommandShellException(String command, CommandResult result) { + this(result); + this.command = command; + } + +}