Skip to content

Commit

Permalink
change: Properly throw an exception on failed addServer() instead of …
Browse files Browse the repository at this point in the history
…returning a null object.
  • Loading branch information
FlorianBauer committed Jan 2, 2023
1 parent e4aaef8 commit 1439411
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ private void connectToServerList(List<String> hostPortStrList) {
try {
hp = HostAndPort.fromString(hostPortStr);
conManager.addServer(hp.getHost(), hp.getPort());
} catch (ServerConnectionException ex) {
} catch (final ServerConnectionException ex) {
System.err.println("Could not connect to '" + hostPortStr + "': " + ex.getMessage());
} catch (IllegalArgumentException ex) {
} catch (final Exception ex) {
System.err.println(ex.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@ public static ConnectionManager getInstance() {
}

/**
* Connect to a server using a trusted certificate or through an unsecure connection
* if {@link ServerManager#setAllowUnsecureConnection(boolean)} has been set to true.
* Note that setting this to true is deprecated and not allowed by the SiLA Standard and should only be used for
* test purposes.
* Connect to a server using a trusted certificate or through an unsecure connection if
* {@link ServerManager#setAllowUnsecureConnection(boolean)} has been set to true. Note that
* setting this to true is deprecated and not allowed by the SiLA Standard and should only be
* used for test purposes.
*
* @param host the server host
* @param port the server host
* @return the server UUID if connection is successful
* @throws ServerConnectionException if unable to connect to the server or is not a valid SiLA Server
* @throws ServerConnectionException if unable to connect to the server or is not a valid SiLA
* Server
*/
public UUID addServer(final String host, int port) throws ServerConnectionException {
public UUID addServer(final String host, int port) throws Exception {
// method is marked as deprecated but is not, sila_java 0.11.0 will fix this problem
serverManager.addServer(host, port);
for (final Server server : serverManager.getServers().values()) {
Expand All @@ -52,21 +54,23 @@ public UUID addServer(final String host, int port) throws ServerConnectionExcept
return serverUuid;
}
}
return null;
throw new Exception("Adding server failed for unknown reason (01).");
}

/**
* Connect to a server using a untrusted (self-signed) certificate or through an unsecure connection
* if {@link ServerManager#setAllowUnsecureConnection(boolean)} has been set to true.
* Note that setting this to true is deprecated and not allowed by the SiLA Standard and should only be used for
* test purposes.
* Connect to a server using a untrusted (self-signed) certificate or through an unsecure
* connection if {@link ServerManager#setAllowUnsecureConnection(boolean)} has been set to true.
* Note that setting this to true is deprecated and not allowed by the SiLA Standard and should
* only be used for test purposes.
*
* @param host the server host
* @param port the server host
* @param cert the server untrusted (self-signed) certificate
* @return the server UUID if connection is successful
* @throws ServerConnectionException if unable to connect to the server or is not a valid SiLA Server
* @throws ServerConnectionException if unable to connect to the server or is not a valid SiLA
* Server
*/
public UUID addServer(final String host, int port, String cert) throws ServerConnectionException {
public UUID addServer(final String host, int port, String cert) throws Exception {
serverManager.addServer(host, port, cert);
for (final Server server : serverManager.getServers().values()) {
if (server.getHost().equals(host) && server.getPort() == port) {
Expand All @@ -77,7 +81,7 @@ public UUID addServer(final String host, int port, String cert) throws ServerCon
return serverUuid;
}
}
return null;
throw new Exception("Adding server failed for unknown reason (02).");
}

private void addServerToContext(final UUID serverUuid, final Server server) {
Expand Down

0 comments on commit 1439411

Please sign in to comment.