Skip to content

Commit 7d6e27d

Browse files
committed
[java] Careful analysis of command executor response to throw a meaningful exception instead of NPE or CCE
1 parent 7882954 commit 7d6e27d

File tree

3 files changed

+122
-69
lines changed

3 files changed

+122
-69
lines changed

java/client/src/org/openqa/selenium/SessionNotCreatedException.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
*/
2323
public class SessionNotCreatedException extends WebDriverException {
2424
public SessionNotCreatedException(String msg) {
25-
super(msg);
25+
super("Could not start a new session. " + msg);
2626
}
2727

2828
public SessionNotCreatedException(String msg, Throwable cause) {
29-
super(msg, cause);
29+
super("Could not start a new session. " + msg, cause);
3030
}
3131
}

java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java

+17
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,23 @@ protected void setSessionId(String opaqueKey) {
210210
protected void startSession(Capabilities capabilities) {
211211
Response response = execute(DriverCommand.NEW_SESSION(capabilities));
212212

213+
if (response == null) {
214+
throw new SessionNotCreatedException(
215+
"The underlying command executor returned a null response.");
216+
}
217+
218+
if (response.getValue() == null) {
219+
throw new SessionNotCreatedException(
220+
"The underlying command executor returned a response without payload: " +
221+
response.toString());
222+
}
223+
224+
if (!(response.getValue() instanceof Map)) {
225+
throw new SessionNotCreatedException(
226+
"The underlying command executor returned a response with a non well formed payload: " +
227+
response.toString());
228+
}
229+
213230
Map<String, Object> rawCapabilities = (Map<String, Object>) response.getValue();
214231
MutableCapabilities returnedCapabilities = new MutableCapabilities();
215232
for (Map.Entry<String, Object> entry : rawCapabilities.entrySet()) {

0 commit comments

Comments
 (0)