40
40
import org .openqa .selenium .Proxy ;
41
41
import org .openqa .selenium .WebDriverException ;
42
42
import org .openqa .selenium .json .Json ;
43
+ import org .openqa .selenium .json .JsonException ;
43
44
44
45
/**
45
46
* This implementation is still in beta, and may change.
@@ -101,8 +102,8 @@ public static SeleniumManager getInstance() {
101
102
*/
102
103
private static String runCommand (String ... command ) {
103
104
LOG .fine (String .format ("Executing Process: %s" , Arrays .toString (command )));
104
- String output = "" ;
105
- int code = 0 ;
105
+ String output ;
106
+ int code ;
106
107
try {
107
108
Process process = new ProcessBuilder (command ).redirectErrorStream (true ).start ();
108
109
process .waitFor ();
@@ -111,35 +112,52 @@ private static String runCommand(String... command) {
111
112
CharStreams .toString (
112
113
new InputStreamReader (process .getInputStream (), StandardCharsets .UTF_8 ));
113
114
} catch (InterruptedException e ) {
114
- LOG .warning (
115
- String .format (
116
- "Interrupted exception running command %s: %s" ,
117
- Arrays .toString (command ), e .getMessage ()));
118
115
Thread .currentThread ().interrupt ();
116
+ throw new WebDriverException (
117
+ "Interrupted while running command: "
118
+ + Arrays .toString (command ),
119
+ e );
119
120
} 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
+ }
124
145
}
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 ) {
128
155
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 );
133
160
}
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
- });
143
161
return jsonOutput .result .message ;
144
162
}
145
163
0 commit comments