Skip to content

Commit 1007a72

Browse files
committed
[java] Reworking devtools connection to handle errors (e.g. unimplemented commands)
1 parent 6cd25d0 commit 1007a72

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

java/client/src/org/openqa/selenium/devtools/Connection.java

+24-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
import com.google.common.collect.HashMultimap;
2121
import com.google.common.collect.ImmutableMap;
2222
import com.google.common.collect.Multimap;
23+
24+
import org.openqa.selenium.WebDriverException;
2325
import org.openqa.selenium.devtools.idealized.target.model.SessionID;
26+
import org.openqa.selenium.internal.Either;
2427
import org.openqa.selenium.internal.Require;
2528
import org.openqa.selenium.json.Json;
2629
import org.openqa.selenium.json.JsonInput;
@@ -60,7 +63,7 @@ public class Connection implements Closeable {
6063
});
6164
private static final AtomicLong NEXT_ID = new AtomicLong(1L);
6265
private final WebSocket socket;
63-
private final Map<Long, Consumer<JsonInput>> methodCallbacks = new LinkedHashMap<>();
66+
private final Map<Long, Consumer<Either<JsonInput, Throwable>>> methodCallbacks = new LinkedHashMap<>();
6467
private final Multimap<Event<?>, Consumer<?>> eventCallbacks = HashMultimap.create();
6568

6669
public Connection(HttpClient client, String url) {
@@ -100,13 +103,17 @@ public <X> CompletableFuture<X> send(SessionID sessionId, Command<X> command) {
100103

101104
CompletableFuture<X> result = new CompletableFuture<>();
102105
if (command.getSendsResponse()) {
103-
methodCallbacks.put(id, NamedConsumer.of(command.getMethod(), input -> {
104-
try {
105-
X value = command.getMapper().apply(input);
106-
result.complete(value);
107-
} catch (Throwable e) {
108-
LOG.log(Level.WARNING, String.format("Unable to map result for %s", command.getMethod()), e);
109-
result.completeExceptionally(e);
106+
methodCallbacks.put(id, NamedConsumer.of(command.getMethod(), inputOrException -> {
107+
if (inputOrException.isLeft()) {
108+
try {
109+
X value = command.getMapper().apply(inputOrException.left());
110+
result.complete(value);
111+
} catch (Throwable e) {
112+
LOG.log(Level.WARNING, String.format("Unable to map result for %s", command.getMethod()), e);
113+
result.completeExceptionally(e);
114+
}
115+
} else {
116+
result.completeExceptionally(inputOrException.right());
110117
}
111118
}));
112119
}
@@ -195,8 +202,9 @@ private void handle(CharSequence data) {
195202
LOG.log(getDebugLogLevel(), () -> String.format("<- %s", asString));
196203

197204
Map<String, Object> raw = JSON.toType(asString, MAP_TYPE);
198-
if (raw.get("id") instanceof Number && raw.get("result") != null) {
199-
Consumer<JsonInput> consumer = methodCallbacks.remove(((Number) raw.get("id")).longValue());
205+
if (raw.get("id") instanceof Number
206+
&& (raw.get("result") != null || raw.get("error") != null)) {
207+
Consumer<Either<JsonInput, Throwable>> consumer = methodCallbacks.remove(((Number) raw.get("id")).longValue());
200208
if (consumer == null) {
201209
return;
202210
}
@@ -207,7 +215,12 @@ private void handle(CharSequence data) {
207215
while (input.hasNext()) {
208216
switch (input.nextName()) {
209217
case "result":
210-
consumer.accept(input);
218+
consumer.accept(Either.left(input));
219+
break;
220+
221+
case "error":
222+
consumer.accept(Either.right(new WebDriverException(asString)));
223+
input.skipValue();
211224
break;
212225

213226
default:

java/client/src/org/openqa/selenium/devtools/DevTools.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,12 @@ public void createSession() {
109109
// Set auto-attach to true and run for the hills.
110110
connection.send(cdpSession, getDomains().target().setAutoAttach()),
111111
// Clear the existing logs
112-
connection.send(cdpSession, getDomains().log().clear()))
113-
.get(timeout.toMillis(), MILLISECONDS);
112+
connection.send(cdpSession, getDomains().log().clear())
113+
.exceptionally(t -> {
114+
t.printStackTrace();
115+
return null;
116+
})
117+
).get(timeout.toMillis(), MILLISECONDS);
114118
} catch (InterruptedException e) {
115119
Thread.currentThread().interrupt();
116120
throw new IllegalStateException("Thread has been interrupted", e);

0 commit comments

Comments
 (0)