Skip to content

Commit 143a1e2

Browse files
committed
Better error message when using wrong port
A common error is to try to connect with bolt on port 7474 which leads to error with a hard-to-grasp error message. This PR improves the error message.
1 parent 389376e commit 143a1e2

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

driver/src/main/java/org/neo4j/driver/internal/connector/socket/SocketClient.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class SocketClient
4040
{
4141
private static final int MAGIC_PREAMBLE = 0x6060B017;
4242
private static final int VERSION1 = 1;
43+
private static final int HTTP = 1213486160;//== 0x48545450 == "HTTP"
4344
private static final int NO_VERSION = 0;
4445
private static final int[] SUPPORTED_VERSIONS = new int[]{VERSION1, NO_VERSION, NO_VERSION, NO_VERSION};
4546

@@ -127,7 +128,8 @@ public void receiveOne( SocketResponseHandler handler ) throws IOException
127128
reader.read( handler );
128129

129130
// TODO: all the errors come from the following trace should result in the termination of this channel
130-
// https://github.com/neo4j/neo4j/blob/3.0/community/bolt/src/main/java/org/neo4j/bolt/v1/transport/BoltProtocolV1.java#L86
131+
// https://github.com/neo4j/neo4j/blob/3
132+
// .0/community/bolt/src/main/java/org/neo4j/bolt/v1/transport/BoltProtocolV1.java#L86
131133
if ( handler.protocolViolationErrorOccurred() )
132134
{
133135
stop();
@@ -139,7 +141,7 @@ public void stop()
139141
{
140142
try
141143
{
142-
if( channel != null )
144+
if ( channel != null )
143145
{
144146
logger.debug( "~~ [CLOSE]" );
145147
channel.close();
@@ -148,13 +150,13 @@ public void stop()
148150
}
149151
catch ( IOException e )
150152
{
151-
if( e.getMessage().equals( "An existing connection was forcibly closed by the remote host" ) )
153+
if ( e.getMessage().equals( "An existing connection was forcibly closed by the remote host" ) )
152154
{
153155
// Swallow this exception as it is caused by connection already closed by server
154156
}
155157
else
156158
{
157-
throw new ClientException("Unable to close socket connection properly." + e.getMessage(), e);
159+
throw new ClientException( "Unable to close socket connection properly." + e.getMessage(), e );
158160
}
159161
}
160162
}
@@ -192,11 +194,17 @@ private SocketProtocol negotiateProtocol() throws IOException
192194
case VERSION1:
193195
logger.debug( "~~ [HANDSHAKE] 1" );
194196
return new SocketProtocolV1( channel );
195-
case NO_VERSION: throw new ClientException( "The server does not support any of the protocol versions supported by " +
196-
"this driver. Ensure that you are using driver and server versions that " +
197-
"are compatible with one another." );
198-
default: throw new ClientException( "Protocol error, server suggested unexpected protocol version: " +
199-
proposal );
197+
case NO_VERSION:
198+
throw new ClientException( "The server does not support any of the protocol versions supported by " +
199+
"this driver. Ensure that you are using driver and server versions that " +
200+
"are compatible with one another." );
201+
case HTTP:
202+
throw new ClientException(
203+
"Server responded HTTP. Make sure you are not trying to connect to the http endpoint " +
204+
"(HTTP defaults to port 7474 whereas BOLT defaults to port 7687)" );
205+
default:
206+
throw new ClientException( "Protocol error, server suggested unexpected protocol version: " +
207+
proposal );
200208
}
201209
}
202210

@@ -236,7 +244,7 @@ public static ByteChannel create( String host, int port, Config config, Logger l
236244
throw new ClientException( "Unknown TLS Level: " + config.encryptionLevel() );
237245
}
238246

239-
if( logger.isTraceEnabled() )
247+
if ( logger.isTraceEnabled() )
240248
{
241249
channel = new LoggingByteChannel( channel, logger );
242250
}

driver/src/test/java/org/neo4j/driver/v1/integration/ErrorIT.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
*/
1919
package org.neo4j.driver.v1.integration;
2020

21-
import java.util.UUID;
22-
2321
import org.junit.Rule;
2422
import org.junit.Test;
2523
import org.junit.rules.ExpectedException;
2624

25+
import java.util.UUID;
26+
27+
import org.neo4j.driver.v1.Config;
2728
import org.neo4j.driver.v1.Driver;
2829
import org.neo4j.driver.v1.GraphDatabase;
2930
import org.neo4j.driver.v1.StatementResult;
@@ -151,5 +152,22 @@ public void shouldHandleFailureAtCommitTime() throws Throwable
151152

152153
}
153154

155+
@Test
156+
public void shouldGetHelpfulErrorWhenTryingToConnectToHttpPort() throws Throwable
157+
{
158+
// Given
159+
//the http server needs some time to start up
160+
Thread.sleep( 2000 );
161+
Driver driver = GraphDatabase.driver( "bolt://localhost:7474", Config.build().withEncryptionLevel(
162+
Config.EncryptionLevel.NONE ).toConfig());
163+
164+
// Expect
165+
exception.expect( ClientException.class );
166+
exception.expectMessage( "Server responded HTTP. Make sure you are not trying to connect to the http endpoint " +
167+
"(HTTP defaults to port 7474 whereas BOLT defaults to port 7687)" );
168+
169+
// When
170+
driver.session();
171+
}
154172

155173
}

driver/src/test/java/org/neo4j/driver/v1/integration/LoadCSVIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.neo4j.driver.v1.integration;
2020

21-
import org.junit.Ignore;
2221
import org.junit.Rule;
2322
import org.junit.Test;
2423

0 commit comments

Comments
 (0)