20
20
import com .google .common .collect .HashMultimap ;
21
21
import com .google .common .collect .ImmutableMap ;
22
22
import com .google .common .collect .Multimap ;
23
+
24
+ import org .openqa .selenium .WebDriverException ;
23
25
import org .openqa .selenium .devtools .idealized .target .model .SessionID ;
26
+ import org .openqa .selenium .internal .Either ;
24
27
import org .openqa .selenium .internal .Require ;
25
28
import org .openqa .selenium .json .Json ;
26
29
import org .openqa .selenium .json .JsonInput ;
@@ -60,7 +63,7 @@ public class Connection implements Closeable {
60
63
});
61
64
private static final AtomicLong NEXT_ID = new AtomicLong (1L );
62
65
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 <>();
64
67
private final Multimap <Event <?>, Consumer <?>> eventCallbacks = HashMultimap .create ();
65
68
66
69
public Connection (HttpClient client , String url ) {
@@ -100,13 +103,17 @@ public <X> CompletableFuture<X> send(SessionID sessionId, Command<X> command) {
100
103
101
104
CompletableFuture <X > result = new CompletableFuture <>();
102
105
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 ());
110
117
}
111
118
}));
112
119
}
@@ -195,8 +202,9 @@ private void handle(CharSequence data) {
195
202
LOG .log (getDebugLogLevel (), () -> String .format ("<- %s" , asString ));
196
203
197
204
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 ());
200
208
if (consumer == null ) {
201
209
return ;
202
210
}
@@ -207,7 +215,12 @@ private void handle(CharSequence data) {
207
215
while (input .hasNext ()) {
208
216
switch (input .nextName ()) {
209
217
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 ();
211
224
break ;
212
225
213
226
default :
0 commit comments