Ver código fonte

throw CommandShellException instead of calling 'die' when errors occur in CommandShell utility class

tags/2.0.1
Jonathan Cobb 4 anos atrás
pai
commit
f76b422bb8
2 arquivos alterados com 35 adições e 7 exclusões
  1. +7
    -7
      src/main/java/org/cobbzilla/util/system/CommandShell.java
  2. +28
    -0
      src/main/java/org/cobbzilla/util/system/CommandShellException.java

+ 7
- 7
src/main/java/org/cobbzilla/util/system/CommandShell.java Ver arquivo

@@ -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<String, String> env, List<Integer> 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;
}



+ 28
- 0
src/main/java/org/cobbzilla/util/system/CommandShellException.java Ver arquivo

@@ -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;
}

}

Carregando…
Cancelar
Salvar