Skip to content

Commit

Permalink
[java] Improved the exit code and error handling (#12219)
Browse files Browse the repository at this point in the history
Improved the exit code and error handling

Co-authored-by: Diego Molina <diemol@users.noreply.github.com>
  • Loading branch information
joerg1985 and diemol committed Jun 23, 2023
1 parent 415f20b commit 70c67ed
Showing 1 changed file with 44 additions and 26 deletions.
70 changes: 44 additions & 26 deletions java/src/org/openqa/selenium/manager/SeleniumManager.java
Expand Up @@ -40,6 +40,7 @@
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonException;

/**
* This implementation is still in beta, and may change.
Expand Down Expand Up @@ -101,8 +102,8 @@ public static SeleniumManager getInstance() {
*/
private static String runCommand(String... command) {
LOG.fine(String.format("Executing Process: %s", Arrays.toString(command)));
String output = "";
int code = 0;
String output;
int code;
try {
Process process = new ProcessBuilder(command).redirectErrorStream(true).start();
process.waitFor();
Expand All @@ -111,35 +112,52 @@ private static String runCommand(String... command) {
CharStreams.toString(
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
} catch (InterruptedException e) {
LOG.warning(
String.format(
"Interrupted exception running command %s: %s",
Arrays.toString(command), e.getMessage()));
Thread.currentThread().interrupt();
throw new WebDriverException(
"Interrupted while running command: "
+ Arrays.toString(command),
e);
} catch (Exception e) {
LOG.warning(
String.format(
"%s running command %s: %s",
e.getClass().getSimpleName(), Arrays.toString(command), e.getMessage()));
throw new WebDriverException(
"Failed to run command: "
+ Arrays.toString(command),
e);
}
SeleniumManagerJsonOutput jsonOutput = null;
JsonException failedToParse = null;
String dump = output;
if (!output.isEmpty()) {
try {
jsonOutput = new Json().toType(output, SeleniumManagerJsonOutput.class);
jsonOutput.logs.forEach(
logged -> {
if (logged.level.equalsIgnoreCase(WARN)) {
LOG.warning(logged.message);
}
if (logged.level.equalsIgnoreCase(DEBUG) || logged.level.equalsIgnoreCase(INFO)) {
LOG.fine(logged.message);
}
});
dump = jsonOutput.result.message;
} catch (JsonException e) {
failedToParse = e;
}
}
SeleniumManagerJsonOutput jsonOutput =
new Json().toType(output, SeleniumManagerJsonOutput.class);
if (code > 0) {
if (code != 0) {
throw new WebDriverException(
"Command failed with code: "
+ code
+ ", executed: "
+ Arrays.toString(command)
+ "\n"
+ dump, failedToParse);
} else if (failedToParse != null) {
throw new WebDriverException(
"Unsuccessful command executed: "
+ Arrays.toString(command)
+ "\n"
+ jsonOutput.result.message);
"Failed to parse json output, executed: "
+ Arrays.toString(command)
+ "\n"
+ dump, failedToParse);
}
jsonOutput.logs.forEach(
logged -> {
if (logged.level.equalsIgnoreCase(WARN)) {
LOG.warning(logged.message);
}
if (logged.level.equalsIgnoreCase(DEBUG) || logged.level.equalsIgnoreCase(INFO)) {
LOG.fine(logged.message);
}
});
return jsonOutput.result.message;
}

Expand Down

0 comments on commit 70c67ed

Please sign in to comment.