Skip to content

Commit 43c4590

Browse files
bigmontzfbiville
andauthored
Add error logs for connecting to HTTP server (#1113)
The driver already throws an appropriated erro when the server returns HTTP. Add this information to the log helps with future debugging process. Co-authored-by: Florent Biville <445792+fbiville@users.noreply.github.com>
1 parent 7e837e1 commit 43c4590

File tree

6 files changed

+42
-11
lines changed

6 files changed

+42
-11
lines changed

packages/bolt-connection/src/bolt/handshake.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,16 @@ function createHandshakeMessage (versions) {
5454
return handshakeBuffer
5555
}
5656

57-
function parseNegotiatedResponse (buffer) {
57+
function parseNegotiatedResponse (buffer, log) {
5858
const h = [
5959
buffer.readUInt8(),
6060
buffer.readUInt8(),
6161
buffer.readUInt8(),
6262
buffer.readUInt8()
6363
]
6464
if (h[0] === 0x48 && h[1] === 0x54 && h[2] === 0x54 && h[3] === 0x50) {
65+
log.error('Handshake failed since server responded with HTTP.')
66+
6567
throw newError(
6668
'Server responded HTTP. Make sure you are not trying to connect to the http endpoint ' +
6769
'(HTTP defaults to port 7474 whereas BOLT defaults to port 7687)'
@@ -97,9 +99,10 @@ function newHandshakeBuffer () {
9799
* Shake hands using the channel and return the protocol version
98100
*
99101
* @param {Channel} channel the channel use to shake hands
102+
* @param {Logger} log the log object
100103
* @returns {Promise<HandshakeResult>} Promise of protocol version and consumeRemainingBuffer
101104
*/
102-
export default function handshake (channel) {
105+
export default function handshake (channel, log) {
103106
return new Promise((resolve, reject) => {
104107
const handshakeErrorHandler = error => {
105108
reject(error)
@@ -113,7 +116,7 @@ export default function handshake (channel) {
113116
channel.onmessage = buffer => {
114117
try {
115118
// read the response buffer and initialize the protocol
116-
const protocolVersion = parseNegotiatedResponse(buffer)
119+
const protocolVersion = parseNegotiatedResponse(buffer, log)
117120

118121
resolve({
119122
protocolVersion,

packages/bolt-connection/src/connection/connection-channel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function createChannelConnection (
5353

5454
const channel = createChannel(channelConfig)
5555

56-
return Bolt.handshake(channel)
56+
return Bolt.handshake(channel, log)
5757
.then(({ protocolVersion: version, consumeRemainingBuffer }) => {
5858
const chunker = new Chunker(channel)
5959
const dechunker = new Dechunker()

packages/bolt-connection/test/bolt/index.test.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('#unit Bolt', () => {
7474
channel.onmessage(packedHandshakeMessage(expectedProtocolVersion))
7575
})
7676

77-
it('should handle a successful handshake with reaining buffer', done => {
77+
it('should handle a successful handshake with remaining buffer', done => {
7878
const { channel, handshakePromise } = subject()
7979
const expectedProtocolVersion = 4.3
8080
const expectedExtraBuffer = createExtraBuffer()
@@ -114,6 +114,29 @@ describe('#unit Bolt', () => {
114114

115115
channel.onmessage(packedHandshakeMessage(httpMagicNumber))
116116
})
117+
118+
it('should log error if the server responds with http payload', async () => {
119+
const { channel, handshakePromise, log } = subject()
120+
const httpMagicNumber = 1213486160
121+
const logErrorSpy = jest.spyOn(log, 'error')
122+
123+
channel.onmessage(packedHandshakeMessage(httpMagicNumber))
124+
125+
await expect(handshakePromise).rejects.toThrow()
126+
expect(logErrorSpy).toHaveBeenCalledWith('Handshake failed since server responded with HTTP.')
127+
})
128+
129+
it('should not log error if the server responds with a valid protocol version', async () => {
130+
const { channel, handshakePromise, log } = subject()
131+
const expectedProtocolVersion = 4.3
132+
const logErrorSpy = jest.spyOn(log, 'error')
133+
134+
channel.onmessage(packedHandshakeMessage(expectedProtocolVersion))
135+
136+
await expect(handshakePromise).resolves.not.toThrow()
137+
expect(logErrorSpy).not.toBeCalled()
138+
})
139+
117140
it('should handle a failed handshake', done => {
118141
const { channel, handshakePromise } = subject()
119142
const expectedError = new Error('Something got wrong')
@@ -143,9 +166,11 @@ describe('#unit Bolt', () => {
143166
})
144167

145168
function subject ({ channel = new DummyChannel() } = {}) {
169+
const log = new Logger('debug', () => {})
146170
return {
171+
log,
147172
channel,
148-
handshakePromise: Bolt.handshake(channel)
173+
handshakePromise: Bolt.handshake(channel, log)
149174
}
150175
}
151176

packages/neo4j-driver-deno/lib/bolt-connection/bolt/handshake.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,16 @@ function createHandshakeMessage (versions) {
5454
return handshakeBuffer
5555
}
5656

57-
function parseNegotiatedResponse (buffer) {
57+
function parseNegotiatedResponse (buffer, log) {
5858
const h = [
5959
buffer.readUInt8(),
6060
buffer.readUInt8(),
6161
buffer.readUInt8(),
6262
buffer.readUInt8()
6363
]
6464
if (h[0] === 0x48 && h[1] === 0x54 && h[2] === 0x54 && h[3] === 0x50) {
65+
log.error('Handshake failed since server responded with HTTP.')
66+
6567
throw newError(
6668
'Server responded HTTP. Make sure you are not trying to connect to the http endpoint ' +
6769
'(HTTP defaults to port 7474 whereas BOLT defaults to port 7687)'
@@ -97,9 +99,10 @@ function newHandshakeBuffer () {
9799
* Shake hands using the channel and return the protocol version
98100
*
99101
* @param {Channel} channel the channel use to shake hands
102+
* @param {Logger} log the log object
100103
* @returns {Promise<HandshakeResult>} Promise of protocol version and consumeRemainingBuffer
101104
*/
102-
export default function handshake (channel) {
105+
export default function handshake (channel, log) {
103106
return new Promise((resolve, reject) => {
104107
const handshakeErrorHandler = error => {
105108
reject(error)
@@ -113,7 +116,7 @@ export default function handshake (channel) {
113116
channel.onmessage = buffer => {
114117
try {
115118
// read the response buffer and initialize the protocol
116-
const protocolVersion = parseNegotiatedResponse(buffer)
119+
const protocolVersion = parseNegotiatedResponse(buffer, log)
117120

118121
resolve({
119122
protocolVersion,

packages/neo4j-driver-deno/lib/bolt-connection/connection/connection-channel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function createChannelConnection (
5353

5454
const channel = createChannel(channelConfig)
5555

56-
return Bolt.handshake(channel)
56+
return Bolt.handshake(channel, log)
5757
.then(({ protocolVersion: version, consumeRemainingBuffer }) => {
5858
const chunker = new Chunker(channel)
5959
const dechunker = new Dechunker()

packages/neo4j-driver/test/internal/connection-channel.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ describe('#integration ChannelConnection', () => {
133133
it('should provide error message when connecting to http-port', async done => {
134134
await createConnection(`bolt://${sharedNeo4j.hostname}:7474`, {
135135
encrypted: false
136-
})
136+
}, null, new Logger('error', () => {}))
137137
.then(done.fail.bind(done))
138138
.catch(error => {
139139
expect(error).toBeDefined()

0 commit comments

Comments
 (0)