Skip to content

Commit 5ae1547

Browse files
committed
[java] Making stack trace shorter and prettier by throwing more specific exception
1 parent 2a10bce commit 5ae1547

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

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

+14-13
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.openqa.selenium.ImmutableCapabilities;
2424
import org.openqa.selenium.Proxy;
2525
import org.openqa.selenium.SessionNotCreatedException;
26-
import org.openqa.selenium.WebDriverException;
2726
import org.openqa.selenium.internal.Either;
2827
import org.openqa.selenium.internal.Require;
2928
import org.openqa.selenium.json.Json;
@@ -60,21 +59,19 @@ public Result createSession(HttpHandler client, Command command) throws IOExcept
6059
desired = desired == null ? new ImmutableCapabilities() : desired;
6160

6261
try (NewSessionPayload payload = NewSessionPayload.create(desired)) {
63-
Either<WebDriverException, Result> result = createSession(client, payload);
62+
Either<SessionNotCreatedException, Result> result = createSession(client, payload);
6463

6564
if (result.isRight()) {
6665
Result toReturn = result.right();
6766
LOG.info(String.format("Detected dialect: %s", toReturn.dialect));
6867
return toReturn;
6968
} else {
70-
throw new SessionNotCreatedException(
71-
String.format("Unable to create new remote session with desired capabilities = %s", desired),
72-
result.left());
69+
throw result.left();
7370
}
7471
}
7572
}
7673

77-
public Either<WebDriverException, Result> createSession(HttpHandler client, NewSessionPayload payload) throws IOException {
74+
public Either<SessionNotCreatedException, Result> createSession(HttpHandler client, NewSessionPayload payload) throws IOException {
7875
int threshold = (int) Math.min(Runtime.getRuntime().freeMemory() / 10, Integer.MAX_VALUE);
7976
FileBackedOutputStream os = new FileBackedOutputStream(threshold);
8077

@@ -91,7 +88,7 @@ public Either<WebDriverException, Result> createSession(HttpHandler client, NewS
9188
}
9289
}
9390

94-
private Either<WebDriverException, Result> createSession(HttpHandler client, InputStream newSessionBlob, long size) {
91+
private Either<SessionNotCreatedException, Result> createSession(HttpHandler client, InputStream newSessionBlob, long size) {
9592
// Create the http request and send it
9693
HttpRequest request = new HttpRequest(HttpMethod.POST, "/session");
9794

@@ -111,7 +108,7 @@ private Either<WebDriverException, Result> createSession(HttpHandler client, Inp
111108
try {
112109
blob = new Json().toType(string(response), Map.class);
113110
} catch (JsonException e) {
114-
return Either.left(new WebDriverException(
111+
return Either.left(new SessionNotCreatedException(
115112
"Unable to parse remote response: " + string(response), e));
116113
}
117114

@@ -121,9 +118,13 @@ private Either<WebDriverException, Result> createSession(HttpHandler client, Inp
121118
blob);
122119

123120
if (initialResponse.getStatusCode() != 200) {
124-
return Either.left(new WebDriverException(
125-
String.format("Server response code %s, message: %s",
126-
initialResponse.getStatusCode(), blob.get("message").toString())));
121+
Object rawResponseValue = initialResponse.getData().get("value");
122+
String responseMessage = rawResponseValue instanceof Map
123+
? ((Map<?, ?>) rawResponseValue).get("message").toString()
124+
: new Json().toJson(rawResponseValue);
125+
return Either.left(new SessionNotCreatedException(
126+
String.format("Response code %s. Message: %s",
127+
initialResponse.getStatusCode(), responseMessage)));
127128
}
128129

129130
return Stream.of(
@@ -132,9 +133,9 @@ private Either<WebDriverException, Result> createSession(HttpHandler client, Inp
132133
.map(func -> func.apply(initialResponse))
133134
.filter(Objects::nonNull)
134135
.findFirst()
135-
.<Either<WebDriverException, Result>>map(Either::right)
136+
.<Either<SessionNotCreatedException, Result>>map(Either::right)
136137
.orElseGet(() -> Either.left(
137-
new WebDriverException("Handshake response does not match any supported protocol")));
138+
new SessionNotCreatedException("Handshake response does not match any supported protocol")));
138139
}
139140

140141
public static class Result {

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,12 @@ protected Response execute(CommandPayload payload) {
632632
log(sessionId, command.getName(), command, When.EXCEPTION);
633633
WebDriverException toThrow;
634634
if (command.getName().equals(DriverCommand.NEW_SESSION)) {
635-
toThrow = new SessionNotCreatedException(
636-
"Possible causes are invalid address of the remote server or browser start-up failure.", e);
635+
if (e instanceof SessionNotCreatedException) {
636+
toThrow = (WebDriverException) e;
637+
} else {
638+
toThrow = new SessionNotCreatedException(
639+
"Possible causes are invalid address of the remote server or browser start-up failure.", e);
640+
}
637641
} else if (e instanceof WebDriverException) {
638642
toThrow = (WebDriverException) e;
639643
} else {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ private WebDriver getRemoteDriver() {
357357
.andThen(new AddWebDriverSpecHeaders())
358358
.andThen(new ErrorFilter()));
359359

360-
Either<WebDriverException, ProtocolHandshake.Result> result = null;
360+
Either<SessionNotCreatedException, ProtocolHandshake.Result> result = null;
361361
try {
362362
result = new ProtocolHandshake().createSession(handler, getPayload());
363363
} catch (IOException e) {
@@ -368,7 +368,7 @@ private WebDriver getRemoteDriver() {
368368
CommandExecutor executor = result.map(res -> createExecutor(handler, res));
369369
return new RemoteWebDriver(executor, new ImmutableCapabilities());
370370
} else {
371-
throw new SessionNotCreatedException("Unable to create new remote session", result.left());
371+
throw result.left();
372372
}
373373
}
374374

0 commit comments

Comments
 (0)