Skip to content
Snippets Groups Projects
Commit 36acadb1 authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

throw exception if command execution fails

parent c08feda1
No related branches found
No related tags found
No related merge requests found
...@@ -49,11 +49,17 @@ public class SshCommandClient extends AbstractBaseSshClient { ...@@ -49,11 +49,17 @@ public class SshCommandClient extends AbstractBaseSshClient {
channelExec.connect(); channelExec.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); BufferedReader reader = new BufferedReader(new InputStreamReader(in));
BufferedReader errReader = new BufferedReader(new InputStreamReader(
channelExec.getErrStream()));
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
result.add(line); result.add(line);
} }
List<String> errors = new LinkedList<>();
while ((line = errReader.readLine()) != null) {
errors.add(line);
}
int exitStatus = channelExec.getExitStatus(); int exitStatus = channelExec.getExitStatus();
channelExec.disconnect(); channelExec.disconnect();
...@@ -62,7 +68,8 @@ public class SshCommandClient extends AbstractBaseSshClient { ...@@ -62,7 +68,8 @@ public class SshCommandClient extends AbstractBaseSshClient {
log.debug("Done, but exit status not set!"); log.debug("Done, but exit status not set!");
} }
else if (exitStatus > 0) { else if (exitStatus > 0) {
log.debug("Done, but with error!"); log.debug("Done, but with error! ");
throw new SshExecuteCommandException(exitStatus, result, errors);
} }
else { else {
log.debug("Done!"); log.debug("Done!");
......
package cz.it4i.fiji.scpclient;
import com.jcraft.jsch.JSchException;
import java.util.List;
public class SshExecuteCommandException extends JSchException {
private int exitStatus;
private List<String> stdout;
private List<String> stderr;
public SshExecuteCommandException(int exitStatus, List<String> stdout,
List<String> stderr)
{
super("exitStatus: " + exitStatus + ", error output: " + String.join("\n",
stderr));
this.exitStatus = exitStatus;
this.stdout = stdout;
this.stderr = stderr;
}
public int getExitStatus() {
return exitStatus;
}
public List<String> getStdout() {
return stdout;
}
public List<String> getStderr() {
return stderr;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment