Skip to content

Commit df314fe

Browse files
author
Zhen Li
committed
Refined the error message when failed to verify connectivity
1 parent 2f7e88c commit df314fe

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

driver/src/main/java/org/neo4j/driver/Driver.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ public interface Driver extends AutoCloseable
170170
* by establishing a network connection with the remote and possibly exchanging a few data before closing the connection.
171171
*
172172
* It throws exception if fails to connect. Use the exception to further understand the cause of the connectivity problem.
173+
* Note: Even if this method throws an exception, the driver still need to be closed via {@link #close()} to free up all resources.
173174
*/
174175
void verifyConnectivity();
175176

@@ -181,6 +182,7 @@ public interface Driver extends AutoCloseable
181182
* {@code null} when the driver connects to the remote server or cluster successfully.
182183
* It is completed exceptionally if the driver failed to connect the remote server or cluster.
183184
* This exception can be used to further understand the cause of the connectivity problem.
185+
* Note: Even if this method complete exceptionally, the driver still need to be closed via {@link #closeAsync()} to free up all resources.
184186
*
185187
* @return a {@link CompletionStage completion stage} that represents the asynchronous verification.
186188
*/

driver/src/main/java/org/neo4j/driver/internal/cluster/RediscoveryImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
*/
5454
public class RediscoveryImpl implements Rediscovery
5555
{
56-
private static final String NO_ROUTERS_AVAILABLE = "Could not perform discovery for database '%s'. No routing servers available.";
56+
private static final String NO_ROUTERS_AVAILABLE = "Could not perform discovery for database '%s'. No routing server available.";
5757

5858
private final BoltServerAddress initialRouter;
5959
private final RoutingSettings settings;

driver/src/main/java/org/neo4j/driver/internal/cluster/loadbalancing/LoadBalancer.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,19 @@ public CompletionStage<Connection> acquireConnection( String databaseName, Acces
8787
@Override
8888
public CompletionStage<Void> verifyConnectivity()
8989
{
90-
return routingTables.refreshRoutingTable( ABSENT_DB_NAME, AccessMode.READ ).thenApply( ignored -> null );
90+
return routingTables.refreshRoutingTable( ABSENT_DB_NAME, AccessMode.READ ).handle( ( ignored, error ) -> {
91+
if ( error != null )
92+
{
93+
Throwable cause = Futures.completionExceptionCause( error );
94+
if ( cause instanceof ServiceUnavailableException )
95+
{
96+
throw Futures.asCompletionException( new ServiceUnavailableException(
97+
"Unable to connect to database, ensure the database is running and that there is a working network connection to it.", cause ) );
98+
}
99+
throw Futures.asCompletionException( cause );
100+
}
101+
return null;
102+
} );
91103
}
92104

93105
@Override

driver/src/test/java/org/neo4j/driver/GraphDatabaseTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import static java.util.Arrays.asList;
3434
import static java.util.concurrent.TimeUnit.MILLISECONDS;
35+
import static org.hamcrest.Matchers.containsString;
3536
import static org.hamcrest.Matchers.is;
3637
import static org.hamcrest.core.IsEqual.equalTo;
3738
import static org.hamcrest.junit.MatcherAssert.assertThat;
@@ -161,6 +162,33 @@ void shouldRespondToInterruptsWhenConnectingToUnresponsiveServer() throws Except
161162
}
162163
}
163164

165+
@Test
166+
void shouldPrintNiceErrorWhenConnectingToUnresponsiveServer() throws Exception
167+
{
168+
int localPort = -1;
169+
try ( ServerSocket serverSocket = new ServerSocket( 0 ) )
170+
{
171+
localPort = serverSocket.getLocalPort();
172+
}
173+
final Driver driver = GraphDatabase.driver( "bolt://localhost:" + localPort, INSECURE_CONFIG );
174+
final ServiceUnavailableException error = assertThrows( ServiceUnavailableException.class, driver::verifyConnectivity );
175+
assertThat( error.getMessage(), containsString( "Unable to connect to" ) );
176+
}
177+
178+
@Test
179+
void shouldPrintNiceRoutingErrorWhenConnectingToUnresponsiveServer() throws Exception
180+
{
181+
int localPort = -1;
182+
try ( ServerSocket serverSocket = new ServerSocket( 0 ) )
183+
{
184+
localPort = serverSocket.getLocalPort();
185+
}
186+
final Driver driver = GraphDatabase.driver( "neo4j://localhost:" + localPort, INSECURE_CONFIG );
187+
final ServiceUnavailableException error = assertThrows( ServiceUnavailableException.class, driver::verifyConnectivity );
188+
error.printStackTrace();
189+
assertThat( error.getMessage(), containsString( "Unable to connect to" ) );
190+
}
191+
164192
@Test
165193
void shouldFailToCreateUnencryptedDriverWhenServerDoesNotRespond() throws IOException
166194
{

driver/src/test/java/org/neo4j/driver/stress/CausalClusteringIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ void sessionCreationShouldFailIfCallingDiscoveryProcedureOnEdgeServer()
169169
ClusterMember readReplica = cluster.anyReadReplica();
170170
final Driver driver = createDriver( readReplica.getRoutingUri() );
171171
ServiceUnavailableException e = assertThrows( ServiceUnavailableException.class, driver::verifyConnectivity );
172-
assertThat( e.getMessage(), containsString( "Could not perform discovery" ) );
172+
assertThat( e.getMessage(), containsString( "Unable to connect to database" ) );
173173
}
174174

175175
// Ensure that Bookmarks work with single instances using a driver created using a bolt[not+routing] URI.

0 commit comments

Comments
 (0)