Skip to content

Commit 70c67ed

Browse files
joerg1985diemol
andauthored
[java] Improved the exit code and error handling (#12219)
Improved the exit code and error handling Co-authored-by: Diego Molina <diemol@users.noreply.github.com>
1 parent 415f20b commit 70c67ed

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

java/src/org/openqa/selenium/manager/SeleniumManager.java

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.openqa.selenium.Proxy;
4141
import org.openqa.selenium.WebDriverException;
4242
import org.openqa.selenium.json.Json;
43+
import org.openqa.selenium.json.JsonException;
4344

4445
/**
4546
* This implementation is still in beta, and may change.
@@ -101,8 +102,8 @@ public static SeleniumManager getInstance() {
101102
*/
102103
private static String runCommand(String... command) {
103104
LOG.fine(String.format("Executing Process: %s", Arrays.toString(command)));
104-
String output = "";
105-
int code = 0;
105+
String output;
106+
int code;
106107
try {
107108
Process process = new ProcessBuilder(command).redirectErrorStream(true).start();
108109
process.waitFor();
@@ -111,35 +112,52 @@ private static String runCommand(String... command) {
111112
CharStreams.toString(
112113
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
113114
} catch (InterruptedException e) {
114-
LOG.warning(
115-
String.format(
116-
"Interrupted exception running command %s: %s",
117-
Arrays.toString(command), e.getMessage()));
118115
Thread.currentThread().interrupt();
116+
throw new WebDriverException(
117+
"Interrupted while running command: "
118+
+ Arrays.toString(command),
119+
e);
119120
} catch (Exception e) {
120-
LOG.warning(
121-
String.format(
122-
"%s running command %s: %s",
123-
e.getClass().getSimpleName(), Arrays.toString(command), e.getMessage()));
121+
throw new WebDriverException(
122+
"Failed to run command: "
123+
+ Arrays.toString(command),
124+
e);
125+
}
126+
SeleniumManagerJsonOutput jsonOutput = null;
127+
JsonException failedToParse = null;
128+
String dump = output;
129+
if (!output.isEmpty()) {
130+
try {
131+
jsonOutput = new Json().toType(output, SeleniumManagerJsonOutput.class);
132+
jsonOutput.logs.forEach(
133+
logged -> {
134+
if (logged.level.equalsIgnoreCase(WARN)) {
135+
LOG.warning(logged.message);
136+
}
137+
if (logged.level.equalsIgnoreCase(DEBUG) || logged.level.equalsIgnoreCase(INFO)) {
138+
LOG.fine(logged.message);
139+
}
140+
});
141+
dump = jsonOutput.result.message;
142+
} catch (JsonException e) {
143+
failedToParse = e;
144+
}
124145
}
125-
SeleniumManagerJsonOutput jsonOutput =
126-
new Json().toType(output, SeleniumManagerJsonOutput.class);
127-
if (code > 0) {
146+
if (code != 0) {
147+
throw new WebDriverException(
148+
"Command failed with code: "
149+
+ code
150+
+ ", executed: "
151+
+ Arrays.toString(command)
152+
+ "\n"
153+
+ dump, failedToParse);
154+
} else if (failedToParse != null) {
128155
throw new WebDriverException(
129-
"Unsuccessful command executed: "
130-
+ Arrays.toString(command)
131-
+ "\n"
132-
+ jsonOutput.result.message);
156+
"Failed to parse json output, executed: "
157+
+ Arrays.toString(command)
158+
+ "\n"
159+
+ dump, failedToParse);
133160
}
134-
jsonOutput.logs.forEach(
135-
logged -> {
136-
if (logged.level.equalsIgnoreCase(WARN)) {
137-
LOG.warning(logged.message);
138-
}
139-
if (logged.level.equalsIgnoreCase(DEBUG) || logged.level.equalsIgnoreCase(INFO)) {
140-
LOG.fine(logged.message);
141-
}
142-
});
143161
return jsonOutput.result.message;
144162
}
145163

0 commit comments

Comments
 (0)