Skip to content

Commit 0630b64

Browse files
committed
Error handling for connecting to a non-routing server
1 parent b359da5 commit 0630b64

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/v1/driver.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Pool from './internal/pool';
2222
import Integer from './integer';
2323
import {connect} from "./internal/connector";
2424
import StreamObserver from './internal/stream-observer';
25-
import {newError} from "./error";
25+
import {newError, SERVICE_UNAVAILABLE} from "./error";
2626
import "babel-polyfill";
2727

2828
let READ = 'READ', WRITE = 'WRITE';
@@ -113,6 +113,13 @@ class Driver {
113113
*/
114114
session(mode) {
115115
let connectionPromise = this._acquireConnection(mode);
116+
connectionPromise.catch((err) => {
117+
if (this.onError && err.code === SERVICE_UNAVAILABLE) {
118+
this.onError(err);
119+
} else {
120+
return Promise.reject(err);
121+
}
122+
});
116123
return this._createSession(connectionPromise, (cb) => {
117124
// This gets called on Session#close(), and is where we return
118125
// the pooled 'connection' instance.

src/v1/routing-driver.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,24 @@ class RoutingDriver extends Driver {
3838
_createSession(connectionPromise, cb) {
3939
return new RoutingSession(connectionPromise, cb, (err, conn) => {
4040
let code = err.code;
41+
let msg = err.message;
4142
if (!code) {
4243
try {
4344
code = err.fields[0].code;
4445
} catch (e) {
4546
code = 'UNKNOWN';
4647
}
4748
}
49+
if (!msg) {
50+
try {
51+
msg = err.fields[0].message;
52+
} catch (e) {
53+
msg = 'Unknown failure occurred';
54+
}
55+
}
56+
//just to simplify later error handling
57+
err.code = code;
58+
err.message = msg;
4859

4960
if (code === SERVICE_UNAVAILABLE || code === SESSION_EXPIRED) {
5061
if (conn) {
@@ -65,8 +76,9 @@ class RoutingDriver extends Driver {
6576
this._clusterView.writers.remove(conn.url);
6677
});
6778
}
68-
6979
return newError("No longer possible to write to server at " + url, SESSION_EXPIRED);
80+
} else {
81+
return err;
7082
}
7183
});
7284
}
@@ -224,8 +236,12 @@ function newClusterView(session) {
224236
}
225237
return new ClusterView(routers, readers, writers, expires);
226238
})
227-
.catch(() => {
239+
.catch((e) => {
240+
if (e.code === 'Neo.ClientError.Procedure.ProcedureNotFound') {
241+
return Promise.reject(newError("Server could not perform routing, make sure you are connecting to a causal cluster", SERVICE_UNAVAILABLE));
242+
} else {
228243
return Promise.reject(newError("No servers could be found at this instant.", SERVICE_UNAVAILABLE));
244+
}
229245
});
230246
}
231247

test/v1/driver.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,22 @@ describe('driver', function() {
111111
var driver = neo4j.driver("bolt://localhost", neo4j.auth.custom("neo4j", "neo4j", "native", "basic", {secret: 42}));
112112

113113
// Expect
114-
driver.onCompleted = function (meta) {
114+
driver.onCompleted = function () {
115+
done();
116+
};
117+
118+
// When
119+
driver.session();
120+
});
121+
122+
it('should fail nicely when connecting with routing to standalone server', function(done) {
123+
// Given
124+
var driver = neo4j.driver("bolt+routing://localhost", neo4j.auth.basic("neo4j", "neo4j"));
125+
126+
// Expect
127+
driver.onError = function (err) {
128+
expect(err.message).toEqual('Server could not perform routing, make sure you are connecting to a causal cluster');
129+
expect(err.code).toEqual(neo4j.error.SERVICE_UNAVAILABLE);
115130
done();
116131
};
117132

0 commit comments

Comments
 (0)