Skip to content

Commit 7134ab2

Browse files
committed
Merge pull request #52 from pontusmelke/1.0-connection-errors
Better handling for connection errors in driver
2 parents ca6409a + 249bd5c commit 7134ab2

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/v1/internal/ch-node.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ const TrustStrategy = {
9191
onSuccess();
9292
}
9393
});
94+
socket.on('error', onFailure);
9495
return socket;
9596
},
9697
TRUST_ON_FIRST_USE : function( opts, onSuccess, onFailure ) {
@@ -139,13 +140,16 @@ const TrustStrategy = {
139140
}
140141
});
141142
});
143+
socket.on('error', onFailure);
142144
return socket;
143145
}
144146
};
145147

146148
function connect( opts, onSuccess, onFailure=(()=>null) ) {
147149
if( opts.encrypted === false ) {
148-
return net.connect(opts.port, opts.host, onSuccess);
150+
var conn = net.connect(opts.port, opts.host, onSuccess);
151+
conn.on('error', onFailure);
152+
return conn;
149153
} else if( TrustStrategy[opts.trust]) {
150154
return TrustStrategy[opts.trust](opts, onSuccess, onFailure);
151155
} else {

src/v1/internal/ch-websocket.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ class WebSocketChannel {
5757
this._ws.binaryType = "arraybuffer";
5858

5959
let self = this;
60+
//All connection errors are not sent to the error handler
61+
//we must also check for dirty close calls
62+
this._ws.onclose = function(e) {
63+
if (!e.wasClean) {
64+
self._handleConnectionError();
65+
}
66+
};
6067
this._ws.onopen = function() {
6168
// Drain all pending messages
6269
let pending = self._pending;

test/v1/driver.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,19 @@ describe('driver', function() {
3232
expect( session ).not.toBeNull();
3333
driver.close();
3434
});
35+
36+
it('should handle connection errors', function(done) {
37+
// Given
38+
var driver = neo4j.driver("bolt://localhoste", neo4j.auth.basic("neo4j", "neo4j"));
39+
40+
// Expect
41+
driver.onError = function (err) {
42+
//the error message is different whether in browser or node
43+
expect(err.message).not.toBeNull();
44+
done();
45+
};
46+
47+
// When
48+
driver.session();
49+
});
3550
});

0 commit comments

Comments
 (0)