Skip to content

Commit

Permalink
Throw an exception if new session returns an error. (#3704)
Browse files Browse the repository at this point in the history
Previously if the http response code was not an error
response, then an exception would not be thrown even
if the response contained an error field or a non-zero
status. This was specifically a problem for Chromedriver.
  • Loading branch information
DrMarcII authored and juangj committed Mar 23, 2017
1 parent 9a70926 commit c9fe229
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions java/client/src/org/openqa/selenium/remote/ProtocolHandshake.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,32 @@ private Optional<Result> createSession(HttpClient client, InputStream newSession
jsonBlob = new HashMap<>();
}

// If the result looks positive, return the result.
Object sessionId = jsonBlob.get("sessionId");
Object value = jsonBlob.get("value");
Object w3cError = jsonBlob.get("error");
Object ossStatus = jsonBlob.get("status");

// If the result was an error that we believe has to do with the remote end failing to start the
// session, create an exception and throw it.
Response tempResponse = null;
if (response.getStatus() != HttpURLConnection.HTTP_OK) {
tempResponse = new Response(null);
tempResponse.setStatus(ErrorCodes.SESSION_NOT_CREATED);
tempResponse.setValue(value);
} else if ("session not created".equals(w3cError)) {
tempResponse = new Response(null);
tempResponse.setStatus(ErrorCodes.SESSION_NOT_CREATED);
tempResponse.setValue(value);
} else if (ossStatus instanceof Number && ((Number) ossStatus).intValue() != 0) {
tempResponse = new Response(null);
tempResponse.setStatus(ErrorCodes.SESSION_NOT_CREATED);
tempResponse.setValue(value);
}

if (tempResponse != null) {
new ErrorHandler().throwIfResponseFailed(tempResponse, 0);
}

Object sessionId = jsonBlob.get("sessionId");
Map<String, ?> capabilities = null;

// The old geckodriver prior to 0.14 returned "value" as the thing containing the session id.
Expand All @@ -342,29 +363,10 @@ private Optional<Result> createSession(HttpClient client, InputStream newSession
capabilities = ((Capabilities) capabilities).asMap();
}

if (response.getStatus() == HttpURLConnection.HTTP_OK) {
if (sessionId != null && capabilities != null) {
Dialect dialect = ossStatus == null ? Dialect.W3C : Dialect.OSS;
return Optional.of(
new Result(dialect, String.valueOf(sessionId), capabilities));
}
}

// If the result was an error that we believe has to do with the remote end failing to start the
// session, create an exception and throw it.
Response tempResponse = null;
if ("session not created".equals(w3cError)) {
tempResponse = new Response(null);
tempResponse.setStatus(ErrorCodes.SESSION_NOT_CREATED);
tempResponse.setValue(jsonBlob);
} else if (ossStatus instanceof Number) {
tempResponse = new Response(null);
tempResponse.setStatus(ErrorCodes.SESSION_NOT_CREATED);
tempResponse.setValue(jsonBlob);
}

if (tempResponse != null) {
new ErrorHandler().throwIfResponseFailed(tempResponse, 0);
// If the result looks positive, return the result.
if (sessionId != null && capabilities != null) {
Dialect dialect = ossStatus == null ? Dialect.W3C : Dialect.OSS;
return Optional.of(new Result(dialect, String.valueOf(sessionId), capabilities));
}

// Otherwise, just return empty.
Expand Down

0 comments on commit c9fe229

Please sign in to comment.