diff --git a/lib/core/error.js b/lib/core/error.js index 200d8a1126..e894a80bef 100644 --- a/lib/core/error.js +++ b/lib/core/error.js @@ -95,9 +95,14 @@ class MongoParseError extends MongoError { */ class MongoTimeoutError extends MongoError { constructor(message, reason) { - super(message); + if (reason && reason.error) { + super(reason.error); + } else { + super(message); + } + this.name = 'MongoTimeoutError'; - if (reason != null) { + if (reason) { this.reason = reason; } } diff --git a/lib/core/sdam/server_selection.js b/lib/core/sdam/server_selection.js index bb01b1d6a9..996aca00fd 100644 --- a/lib/core/sdam/server_selection.js +++ b/lib/core/sdam/server_selection.js @@ -261,7 +261,7 @@ function selectServers(topology, selector, timeout, start, callback) { if (duration >= timeout) { return callback( new MongoTimeoutError(`Server selection timed out after ${timeout} ms`), - topology.description.error + topology.description ); } @@ -308,7 +308,7 @@ function selectServers(topology, selector, timeout, start, callback) { callback( new MongoTimeoutError( `Server selection timed out after ${timeout} ms`, - topology.description.error + topology.description ) ); }, timeout - duration); diff --git a/test/functional/core/server.test.js b/test/functional/core/server.test.js index 0c2a8e2aeb..c758ad4279 100644 --- a/test/functional/core/server.test.js +++ b/test/functional/core/server.test.js @@ -1002,8 +1002,7 @@ describe('Server tests', function() { let err; try { expect(error).to.be.an.instanceOf(Error); - const errorMessage = error.reason ? error.reason.message : error.message; - expect(errorMessage).to.match(/but this version of the Node.js Driver requires/); + expect(error).to.match(/but this version of the Node.js Driver requires/); } catch (e) { err = e; } diff --git a/test/functional/scram_sha_256.test.js b/test/functional/scram_sha_256.test.js index 36d3d41eb7..76edef5a50 100644 --- a/test/functional/scram_sha_256.test.js +++ b/test/functional/scram_sha_256.test.js @@ -186,10 +186,7 @@ describe('SCRAM-SHA-256 auth', function() { return withClient( this.configuration.newClient({}, options), () => Promise.reject(new Error('This request should have failed to authenticate')), - err => { - const errMessage = err.reason ? err.reason.message : err; - expect(errMessage).to.match(/Authentication failed/); - } + err => expect(err).to.match(/Authentication failed/) ); } }); @@ -223,10 +220,7 @@ describe('SCRAM-SHA-256 auth', function() { withClient( this.configuration.newClient({}, options), () => Promise.reject(new Error('This request should have failed to authenticate')), - err => { - const errMessage = err.reason ? err.reason.message : err; - expect(errMessage).to.match(/Authentication failed/); - } + err => expect(err).to.match(/Authentication failed/) ); return Promise.all([getErrorMsg(noUsernameOptions), getErrorMsg(badPasswordOptions)]); diff --git a/test/unit/sdam/server_selection/select_servers.test.js b/test/unit/sdam/server_selection/select_servers.test.js index fe55235545..9561b38960 100644 --- a/test/unit/sdam/server_selection/select_servers.test.js +++ b/test/unit/sdam/server_selection/select_servers.test.js @@ -35,7 +35,7 @@ describe('selectServers', function() { selectServers(topology, ReadPreference.primary, 500, process.hrtime(), err => { expect(err).to.exist; expect(err).to.match(/Server selection timed out/); - expect(err).to.not.have.property('reason'); + expect(err).to.have.property('reason'); done(); }); @@ -60,7 +60,7 @@ describe('selectServers', function() { selectServers(topology, ReadPreference.primary, 1000, process.hrtime(), err => { expect(err).to.exist; expect(err).to.match(/Server selection timed out/); - expect(err).to.not.have.property('reason'); + expect(err).to.have.property('reason'); // expect a call to monitor for initial server creation, and another for the server selection expect(serverMonitor)