Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] Raise a ConnectionFailedException when openSocket failed #12215

Merged
merged 3 commits into from
Jun 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 31 additions & 41 deletions java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.openqa.selenium.remote.http.BinaryMessage;
import org.openqa.selenium.remote.http.ClientConfig;
import org.openqa.selenium.remote.http.CloseMessage;
import org.openqa.selenium.remote.http.ConnectionFailedException;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpClientName;
import org.openqa.selenium.remote.http.HttpMethod;
Expand Down Expand Up @@ -145,7 +146,13 @@ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {

@Override
public WebSocket openSocket(HttpRequest request, WebSocket.Listener listener) {
URI uri = getWebSocketUri(request);
URI uri;

try {
uri = getWebSocketUri(request);
} catch (URISyntaxException e) {
throw new ConnectionFailedException("JdkWebSocket initial request execution error", e);
}

CompletableFuture<java.net.http.WebSocket> webSocketCompletableFuture =
client
Expand Down Expand Up @@ -220,25 +227,16 @@ public void onError(java.net.http.WebSocket webSocket, Throwable error) {
underlyingSocket =
webSocketCompletableFuture.get(readTimeout.toMillis(), TimeUnit.MILLISECONDS);
} catch (CancellationException e) {
throw new WebDriverException(e.getMessage(), e);
throw new ConnectionFailedException("JdkWebSocket initial request canceled", e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();

if (cause instanceof HttpTimeoutException) {
throw new TimeoutException(cause);
} else if (cause instanceof IOException) {
throw new UncheckedIOException((IOException) cause);
} else if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}

throw new WebDriverException((cause != null) ? cause : e);
throw new ConnectionFailedException("JdkWebSocket initial request execution error", (cause != null) ? cause : e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
throw new ConnectionFailedException("JdkWebSocket initial request interrupted", e);
} catch (java.util.concurrent.TimeoutException e) {
webSocketCompletableFuture.cancel(true);
throw new TimeoutException(e);
throw new ConnectionFailedException("JdkWebSocket initial request timeout", e);
}

WebSocket websocket =
Expand Down Expand Up @@ -314,36 +312,28 @@ public void close() {
return websocket;
}

private URI getWebSocketUri(HttpRequest request) {
private URI getWebSocketUri(HttpRequest request) throws URISyntaxException {
URI uri = messages.getRawUri(request);
if ("http".equalsIgnoreCase(uri.getScheme())) {
try {
uri =
new URI(
"ws",
uri.getUserInfo(),
uri.getHost(),
uri.getPort(),
uri.getPath(),
uri.getQuery(),
uri.getFragment());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
uri =
new URI(
"ws",
uri.getUserInfo(),
uri.getHost(),
uri.getPort(),
uri.getPath(),
uri.getQuery(),
uri.getFragment());
} else if ("https".equalsIgnoreCase(uri.getScheme())) {
try {
uri =
new URI(
"wss",
uri.getUserInfo(),
uri.getHost(),
uri.getPort(),
uri.getPath(),
uri.getQuery(),
uri.getFragment());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
uri =
new URI(
"wss",
uri.getUserInfo(),
uri.getHost(),
uri.getPort(),
uri.getPath(),
uri.getQuery(),
uri.getFragment());
}
return uri;
}
Expand Down