Skip to content

Better error message when using wrong port #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class SocketClient
{
private static final int MAGIC_PREAMBLE = 0x6060B017;
private static final int VERSION1 = 1;
private static final int HTTP = 1213486160;//== 0x48545450 == "HTTP"
private static final int NO_VERSION = 0;
private static final int[] SUPPORTED_VERSIONS = new int[]{VERSION1, NO_VERSION, NO_VERSION, NO_VERSION};

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

// TODO: all the errors come from the following trace should result in the termination of this channel
// https://github.com/neo4j/neo4j/blob/3.0/community/bolt/src/main/java/org/neo4j/bolt/v1/transport/BoltProtocolV1.java#L86
// https://github.com/neo4j/neo4j/blob/3
// .0/community/bolt/src/main/java/org/neo4j/bolt/v1/transport/BoltProtocolV1.java#L86
if ( handler.protocolViolationErrorOccurred() )
{
stop();
Expand All @@ -139,7 +141,7 @@ public void stop()
{
try
{
if( channel != null )
if ( channel != null )
{
logger.debug( "~~ [CLOSE]" );
channel.close();
Expand All @@ -148,13 +150,13 @@ public void stop()
}
catch ( IOException e )
{
if( e.getMessage().equals( "An existing connection was forcibly closed by the remote host" ) )
if ( e.getMessage().equals( "An existing connection was forcibly closed by the remote host" ) )
{
// Swallow this exception as it is caused by connection already closed by server
}
else
{
throw new ClientException("Unable to close socket connection properly." + e.getMessage(), e);
throw new ClientException( "Unable to close socket connection properly." + e.getMessage(), e );
}
}
}
Expand Down Expand Up @@ -192,11 +194,17 @@ private SocketProtocol negotiateProtocol() throws IOException
case VERSION1:
logger.debug( "~~ [HANDSHAKE] 1" );
return new SocketProtocolV1( channel );
case NO_VERSION: throw new ClientException( "The server does not support any of the protocol versions supported by " +
"this driver. Ensure that you are using driver and server versions that " +
"are compatible with one another." );
default: throw new ClientException( "Protocol error, server suggested unexpected protocol version: " +
proposal );
case NO_VERSION:
throw new ClientException( "The server does not support any of the protocol versions supported by " +
"this driver. Ensure that you are using driver and server versions that " +
"are compatible with one another." );
case HTTP:
throw new ClientException(
"Server responded HTTP. Make sure you are not trying to connect to the http endpoint " +
"(HTTP defaults to port 7474 whereas BOLT defaults to port 7687)" );
default:
throw new ClientException( "Protocol error, server suggested unexpected protocol version: " +
proposal );
}
}

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

if( logger.isTraceEnabled() )
if ( logger.isTraceEnabled() )
{
channel = new LoggingByteChannel( channel, logger );
}
Expand Down
22 changes: 20 additions & 2 deletions driver/src/test/java/org/neo4j/driver/v1/integration/ErrorIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
*/
package org.neo4j.driver.v1.integration;

import java.util.UUID;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.UUID;

import org.neo4j.driver.v1.Config;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.StatementResult;
Expand Down Expand Up @@ -151,5 +152,22 @@ public void shouldHandleFailureAtCommitTime() throws Throwable

}

@Test
public void shouldGetHelpfulErrorWhenTryingToConnectToHttpPort() throws Throwable
{
// Given
//the http server needs some time to start up
Thread.sleep( 2000 );
Driver driver = GraphDatabase.driver( "bolt://localhost:7474", Config.build().withEncryptionLevel(
Config.EncryptionLevel.NONE ).toConfig());

// Expect
exception.expect( ClientException.class );
exception.expectMessage( "Server responded HTTP. Make sure you are not trying to connect to the http endpoint " +
"(HTTP defaults to port 7474 whereas BOLT defaults to port 7687)" );

// When
driver.session();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.neo4j.driver.v1.integration;

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;

Expand Down