Skip to content

Commit e648db8

Browse files
joerg1985diemol
andauthored
[java] Raise a ConnectionFailedException when openSocket failed (#12215)
Raise a ConnectionFailedException when openSocket failed Co-authored-by: Diego Molina <[email protected]>
1 parent 7e0210c commit e648db8

1 file changed

Lines changed: 31 additions & 41 deletions

File tree

java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.openqa.selenium.remote.http.BinaryMessage;
5555
import org.openqa.selenium.remote.http.ClientConfig;
5656
import org.openqa.selenium.remote.http.CloseMessage;
57+
import org.openqa.selenium.remote.http.ConnectionFailedException;
5758
import org.openqa.selenium.remote.http.HttpClient;
5859
import org.openqa.selenium.remote.http.HttpClientName;
5960
import org.openqa.selenium.remote.http.HttpMethod;
@@ -145,7 +146,13 @@ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
145146

146147
@Override
147148
public WebSocket openSocket(HttpRequest request, WebSocket.Listener listener) {
148-
URI uri = getWebSocketUri(request);
149+
URI uri;
150+
151+
try {
152+
uri = getWebSocketUri(request);
153+
} catch (URISyntaxException e) {
154+
throw new ConnectionFailedException("JdkWebSocket initial request execution error", e);
155+
}
149156

150157
CompletableFuture<java.net.http.WebSocket> webSocketCompletableFuture =
151158
client
@@ -220,25 +227,16 @@ public void onError(java.net.http.WebSocket webSocket, Throwable error) {
220227
underlyingSocket =
221228
webSocketCompletableFuture.get(readTimeout.toMillis(), TimeUnit.MILLISECONDS);
222229
} catch (CancellationException e) {
223-
throw new WebDriverException(e.getMessage(), e);
230+
throw new ConnectionFailedException("JdkWebSocket initial request canceled", e);
224231
} catch (ExecutionException e) {
225232
Throwable cause = e.getCause();
226-
227-
if (cause instanceof HttpTimeoutException) {
228-
throw new TimeoutException(cause);
229-
} else if (cause instanceof IOException) {
230-
throw new UncheckedIOException((IOException) cause);
231-
} else if (cause instanceof RuntimeException) {
232-
throw (RuntimeException) cause;
233-
}
234-
235-
throw new WebDriverException((cause != null) ? cause : e);
233+
throw new ConnectionFailedException("JdkWebSocket initial request execution error", (cause != null) ? cause : e);
236234
} catch (InterruptedException e) {
237235
Thread.currentThread().interrupt();
238-
throw new RuntimeException(e);
236+
throw new ConnectionFailedException("JdkWebSocket initial request interrupted", e);
239237
} catch (java.util.concurrent.TimeoutException e) {
240238
webSocketCompletableFuture.cancel(true);
241-
throw new TimeoutException(e);
239+
throw new ConnectionFailedException("JdkWebSocket initial request timeout", e);
242240
}
243241

244242
WebSocket websocket =
@@ -314,36 +312,28 @@ public void close() {
314312
return websocket;
315313
}
316314

317-
private URI getWebSocketUri(HttpRequest request) {
315+
private URI getWebSocketUri(HttpRequest request) throws URISyntaxException {
318316
URI uri = messages.getRawUri(request);
319317
if ("http".equalsIgnoreCase(uri.getScheme())) {
320-
try {
321-
uri =
322-
new URI(
323-
"ws",
324-
uri.getUserInfo(),
325-
uri.getHost(),
326-
uri.getPort(),
327-
uri.getPath(),
328-
uri.getQuery(),
329-
uri.getFragment());
330-
} catch (URISyntaxException e) {
331-
throw new RuntimeException(e);
332-
}
318+
uri =
319+
new URI(
320+
"ws",
321+
uri.getUserInfo(),
322+
uri.getHost(),
323+
uri.getPort(),
324+
uri.getPath(),
325+
uri.getQuery(),
326+
uri.getFragment());
333327
} else if ("https".equalsIgnoreCase(uri.getScheme())) {
334-
try {
335-
uri =
336-
new URI(
337-
"wss",
338-
uri.getUserInfo(),
339-
uri.getHost(),
340-
uri.getPort(),
341-
uri.getPath(),
342-
uri.getQuery(),
343-
uri.getFragment());
344-
} catch (URISyntaxException e) {
345-
throw new RuntimeException(e);
346-
}
328+
uri =
329+
new URI(
330+
"wss",
331+
uri.getUserInfo(),
332+
uri.getHost(),
333+
uri.getPort(),
334+
uri.getPath(),
335+
uri.getQuery(),
336+
uri.getFragment());
347337
}
348338
return uri;
349339
}

0 commit comments

Comments
 (0)