Skip to content

Commit

Permalink
[java] Deleting too general "throws Exception" from method declaratio…
Browse files Browse the repository at this point in the history
…ns, and handling exceptions closer to the place where they can be thrown
  • Loading branch information
barancev committed Apr 14, 2020
1 parent 5da18ea commit c3dde85
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion java/server/src/org/openqa/selenium/cli/CliCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ default boolean isShown() {

interface Executable {

void run() throws Exception;
void run();
}

}
6 changes: 3 additions & 3 deletions java/server/src/org/openqa/selenium/grid/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class Main {
private final PrintStream err;
private final String[] args;

public static void main(String[] args) throws Exception {
public static void main(String[] args) {
new Main(System.out, System.err, args).go();
}

Expand All @@ -60,7 +60,7 @@ public static void main(String[] args) throws Exception {
this.args = args;
}

void go() throws Exception {
void go() {
// It's not private to make it visible for tests
if (args.length == 0) {
new Help(loadCommands(Main.class.getClassLoader())).configure(out, err).run();
Expand Down Expand Up @@ -125,7 +125,7 @@ private static Set<CliCommand> loadCommands(ClassLoader loader) {
return commands;
}

private void launch(String[] args, ClassLoader loader) throws Exception {
private void launch(String[] args, ClassLoader loader) {
String commandName = args[0];
String[] remainingArgs = new String[args.length - 1];
System.arraycopy(args, 1, remainingArgs, 0, args.length - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ public final Executable configure(PrintStream out, PrintStream err, String... ar

protected abstract Config getDefaultConfig();

protected abstract void execute(Config config) throws Exception;
protected abstract void execute(Config config);
}
13 changes: 11 additions & 2 deletions java/server/src/org/openqa/selenium/grid/commands/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import org.openqa.selenium.netty.server.NettyServer;
import org.openqa.selenium.remote.http.HttpClient;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
import java.util.logging.Logger;

Expand Down Expand Up @@ -78,7 +80,7 @@ protected Config getDefaultConfig() {
}

@Override
protected void execute(Config config) throws Exception {
protected void execute(Config config) {
LoggingOptions loggingOptions = new LoggingOptions(config);
Tracer tracer = loggingOptions.getTracer();

Expand All @@ -92,9 +94,16 @@ protected void execute(Config config) throws Exception {

BaseServerOptions serverOptions = new BaseServerOptions(config);

URL externalUrl;
try {
externalUrl = serverOptions.getExternalUri().toURL();
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}

NetworkOptions networkOptions = new NetworkOptions(config);
HttpClient.Factory clientFactory = new RoutableHttpClientFactory(
serverOptions.getExternalUri().toURL(),
externalUrl,
handler,
networkOptions.getHttpClientFactory(tracer));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import org.openqa.selenium.cli.CliCommand;
import org.openqa.selenium.cli.WrappedPrintWriter;
Expand Down Expand Up @@ -90,7 +91,12 @@ public Executable configure(PrintStream out, PrintStream err, String... args) {
}

String path = getClass().getPackage().getName().replaceAll("\\.", "/") + "/" + toDisplay;
String content = readContent(path);
String content;
try {
content = readContent(path);
} catch (IOException e) {
throw new UncheckedIOException(e);
}

PrintWriter outWriter = new WrappedPrintWriter(out, 72, 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected Config getDefaultConfig() {
}

@Override
protected void execute(Config config) throws Exception {
protected void execute(Config config) {
EventBusOptions events = new EventBusOptions(config);
// We need this reference to stop the bus being garbage collected. Which would be less than ideal.
EventBus bus = events.getEventBus();
Expand All @@ -106,10 +106,5 @@ protected void execute(Config config) throws Exception {
info.getReleaseLabel(),
info.getBuildRevision(),
server.getUrl()));

// If we exit, the bus goes out of scope, and it's closed
Thread.currentThread().join();

LOG.info("Shutting down: " + bus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@
import org.openqa.selenium.netty.server.NettyServer;
import org.openqa.selenium.remote.http.HttpClient;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Set;
import java.util.logging.Logger;

Expand Down Expand Up @@ -89,7 +91,7 @@ protected Config getDefaultConfig() {
}

@Override
protected void execute(Config config) throws Exception {
protected void execute(Config config) {
LoggingOptions loggingOptions = new LoggingOptions(config);
Tracer tracer = loggingOptions.getTracer();

Expand All @@ -105,17 +107,19 @@ protected void execute(Config config) throws Exception {

int port = config.getInt("server", "port")
.orElseThrow(() -> new IllegalArgumentException("No port to use configured"));
URI localhost = null;
URI localhost;
URL localhostURL;
try {
localhost = new URI("http", null, hostName, port, null, null, null);
} catch (URISyntaxException e) {
localhostURL = localhost.toURL();
} catch (URISyntaxException | MalformedURLException e) {
throw new IllegalArgumentException(e);
}

NetworkOptions networkOptions = new NetworkOptions(config);
CombinedHandler combinedHandler = new CombinedHandler();
HttpClient.Factory clientFactory = new RoutableHttpClientFactory(
localhost.toURL(),
localhostURL,
combinedHandler,
networkOptions.getHttpClientFactory(tracer));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected Config getDefaultConfig() {
}

@Override
protected void execute(Config config) throws Exception {
protected void execute(Config config) {
LoggingOptions loggingOptions = new LoggingOptions(config);
Tracer tracer = loggingOptions.getTracer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected Config getDefaultConfig() {
}

@Override
protected void execute(Config config) throws Exception {
protected void execute(Config config) {
LoggingOptions loggingOptions = new LoggingOptions(config);
Tracer tracer = loggingOptions.getTracer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected Config getDefaultConfig() {
}

@Override
protected void execute(Config config) throws Exception {
protected void execute(Config config) {
LoggingOptions loggingOptions = new LoggingOptions(config);
Tracer tracer = loggingOptions.getTracer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected Config getDefaultConfig() {
}

@Override
protected void execute(Config config) throws Exception {
protected void execute(Config config) {
SessionMapOptions sessionMapOptions = new SessionMapOptions(config);
SessionMap sessions = sessionMapOptions.getSessionMap();

Expand Down

0 comments on commit c3dde85

Please sign in to comment.