Skip to content

Commit 04a681b

Browse files
authored
[refactoring] improve handling exceptions (#10068)
* [refactoring] improve handling exceptions * throw "new UncheckedIOException(ioe)"" instead of just "new RuntimeException(ioe)" * avoid catching too broad Exception which causes unneeded re-wrapping of RuntimeExceptions * revert changes in com.thoughtworks package .. as it's deprecated and will be removed anyway.
1 parent e70e223 commit 04a681b

9 files changed

Lines changed: 22 additions & 22 deletions

File tree

java/src/org/openqa/selenium/net/LinuxEphemeralPortRangeDetector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.io.Reader;
2424
import java.io.StringReader;
25+
import java.io.UncheckedIOException;
2526
import java.nio.charset.Charset;
2627
import java.nio.file.Files;
2728

@@ -36,7 +37,7 @@ public static LinuxEphemeralPortRangeDetector getInstance() {
3637
try (Reader inputFil = Files.newBufferedReader(file.toPath(), Charset.defaultCharset())) {
3738
return new LinuxEphemeralPortRangeDetector(inputFil);
3839
} catch (IOException e) {
39-
throw new RuntimeException(e);
40+
throw new UncheckedIOException(e);
4041
}
4142
}
4243
return new LinuxEphemeralPortRangeDetector(new StringReader("49152 65535"));

java/src/org/openqa/selenium/net/PortProber.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.openqa.selenium.Platform;
2121

2222
import java.io.IOException;
23+
import java.io.UncheckedIOException;
2324
import java.net.ConnectException;
2425
import java.net.InetSocketAddress;
2526
import java.net.ServerSocket;
@@ -113,7 +114,7 @@ public static void waitForPortUp(int port, int timeout, TimeUnit unit) {
113114
} catch (ConnectException | SocketTimeoutException e) {
114115
// Ignore this
115116
} catch (IOException e) {
116-
throw new RuntimeException(e);
117+
throw new UncheckedIOException(e);
117118
}
118119
}
119120
}

java/src/org/openqa/selenium/net/UrlChecker.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package org.openqa.selenium.net;
1919

20+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
21+
import static java.util.concurrent.TimeUnit.NANOSECONDS;
22+
2023
import java.io.IOException;
2124
import java.net.HttpURLConnection;
2225
import java.net.URL;
@@ -29,9 +32,6 @@
2932
import java.util.concurrent.atomic.AtomicInteger;
3033
import java.util.logging.Logger;
3134

32-
import static java.util.concurrent.TimeUnit.MILLISECONDS;
33-
import static java.util.concurrent.TimeUnit.NANOSECONDS;
34-
3535
/**
3636
* Polls a URL until a HTTP 200 response is received.
3737
*/
@@ -130,9 +130,7 @@ public void waitUntilUnavailable(long timeout, TimeUnit unit, final URL url)
130130
throw new TimeoutException(String.format(
131131
"Timed out waiting for %s to become unavailable after %d ms",
132132
url, MILLISECONDS.convert(System.nanoTime() - start, NANOSECONDS)), e);
133-
} catch (RuntimeException e) {
134-
throw e;
135-
} catch (Exception e) {
133+
} catch (InterruptedException | ExecutionException e) {
136134
throw new RuntimeException(e);
137135
}
138136
}

java/src/org/openqa/selenium/remote/server/log/SessionLogsToFileRepository.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.IOException;
2626
import java.io.ObjectInputStream;
2727
import java.io.ObjectOutputStream;
28+
import java.io.UncheckedIOException;
2829
import java.util.ArrayList;
2930
import java.util.HashMap;
3031
import java.util.List;
@@ -122,7 +123,7 @@ public void removeLogFile(SessionId sessionId) {
122123
try {
123124
logFile.removeLogFile();
124125
} catch (IOException e) {
125-
throw new RuntimeException(e);
126+
throw new UncheckedIOException(e);
126127
}
127128
}
128129

java/src/org/openqa/selenium/server/htmlrunner/HTMLSuiteResult.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.IOException;
2121
import java.io.StringReader;
22+
import java.io.UncheckedIOException;
2223
import java.util.ArrayList;
2324
import java.util.List;
2425

@@ -48,7 +49,7 @@ public HTMLSuiteResult(String originalSuite) {
4849
parser.parse(s, p, true);
4950
} catch (IOException e) {
5051
// DGF aw, this won't really happen! (will it?)
51-
throw new RuntimeException(e);
52+
throw new UncheckedIOException(e);
5253
}
5354
hrefs = p.hrefList;
5455
StringBuilder sb = new StringBuilder();

java/test/org/openqa/selenium/environment/GlobalTestEnvironment.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,8 @@ public static TestEnvironment get() {
3737
public static synchronized TestEnvironment getOrCreate(
3838
Supplier<TestEnvironment> startThisIfNothingIsAlreadyRunning) {
3939
if (environment == null) {
40-
try {
41-
environment = startThisIfNothingIsAlreadyRunning.get();
42-
environment.assertIsValid();
43-
} catch (Exception e) {
44-
throw new RuntimeException(e);
45-
}
40+
environment = startThisIfNothingIsAlreadyRunning.get();
41+
environment.assertIsValid();
4642
}
4743
return environment;
4844
}

java/test/org/openqa/selenium/net/StubNetworkInterfaceProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717
package org.openqa.selenium.net;
1818

19+
import java.io.UncheckedIOException;
1920
import java.net.InetAddress;
2021
import java.net.UnknownHostException;
2122
import java.util.Arrays;
@@ -47,7 +48,7 @@ private static InetAddress inetAddress(String host, String addressString) {
4748
InetAddress tmp = InetAddress.getByName(addressString);
4849
return InetAddress.getByAddress(host, tmp.getAddress());
4950
} catch (UnknownHostException e) {
50-
throw new RuntimeException(e);
51+
throw new UncheckedIOException(e);
5152
}
5253
}
5354

@@ -58,7 +59,7 @@ private static InetAddress inetAddress(String addressString) {
5859
InetAddress tmp = InetAddress.getByName(addressString);
5960
return InetAddress.getByAddress(addressString, tmp.getAddress());
6061
} catch (UnknownHostException e) {
61-
throw new RuntimeException(e);
62+
throw new UncheckedIOException(e);
6263
}
6364
}
6465

java/test/org/openqa/selenium/remote/tracing/opentelemetry/TracerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ public void cleverShenanigansRepresentingWhatWeSeeInTheRouter() {
570570
}
571571
});
572572
toReturn.get();
573-
} catch (Exception e) {
573+
} catch (InterruptedException | ExecutionException e) {
574574
throw new RuntimeException(e);
575575
}
576576
return new HttpResponse();

java/test/org/openqa/selenium/testing/drivers/GridSupplier.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.openqa.selenium.testing.drivers;
1919

20+
import static org.openqa.selenium.remote.http.Contents.string;
21+
2022
import org.openqa.selenium.Capabilities;
2123
import org.openqa.selenium.WebDriver;
2224
import org.openqa.selenium.firefox.FirefoxOptions;
@@ -31,13 +33,12 @@
3133
import org.openqa.selenium.support.ui.Wait;
3234

3335
import java.io.IOException;
36+
import java.io.UncheckedIOException;
3437
import java.net.URL;
3538
import java.time.Duration;
3639
import java.util.Map;
3740
import java.util.function.Supplier;
3841

39-
import static org.openqa.selenium.remote.http.Contents.string;
40-
4142
public class GridSupplier implements Supplier<WebDriver> {
4243

4344
private static OutOfProcessSeleniumServer hub;
@@ -80,7 +81,7 @@ private synchronized void startServers() {
8081
"-role", "node",
8182
"-hub", String.valueOf(hubUrl));
8283
} catch (IOException e) {
83-
throw new RuntimeException(e);
84+
throw new UncheckedIOException(e);
8485
}
8586

8687
// Keep polling the status page of the hub until it claims to be ready

0 commit comments

Comments
 (0)