Skip to content

Commit 0cf7ec9

Browse files
committed
feat: introduce MongoServerSelectionError
This error type subclasses the `MongoTimeout` error, but provides more context that the error was specific to an issue with server selection.
1 parent aee8f57 commit 0cf7ec9

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const connect = require('./lib/mongo_client').connect;
1111
connect.MongoError = core.MongoError;
1212
connect.MongoNetworkError = core.MongoNetworkError;
1313
connect.MongoTimeoutError = core.MongoTimeoutError;
14+
connect.MongoServerSelectionError = core.MongoServerSelectionError;
1415
connect.MongoParseError = core.MongoParseError;
1516
connect.MongoWriteConcernError = core.MongoWriteConcernError;
1617
connect.MongoBulkWriteError = require('./lib/bulk/common').BulkWriteError;

lib/core/error.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class MongoParseError extends MongoError {
9696
class MongoTimeoutError extends MongoError {
9797
constructor(message, reason) {
9898
if (reason && reason.error) {
99-
super(reason.error);
99+
super(reason.error.message || reason.error);
100100
} else {
101101
super(message);
102102
}
@@ -108,6 +108,22 @@ class MongoTimeoutError extends MongoError {
108108
}
109109
}
110110

111+
/**
112+
* An error signifying a client-side server selection error
113+
*
114+
* @param {Error|string|object} message The error message
115+
* @param {string|object} [reason] The reason the timeout occured
116+
* @property {string} message The error message
117+
* @property {string} [reason] An optional reason context for the timeout, generally an error saved during flow of monitoring and selecting servers
118+
* @extends MongoError
119+
*/
120+
class MongoServerSelectionError extends MongoTimeoutError {
121+
constructor(message, reason) {
122+
super(message, reason);
123+
this.name = 'MongoServerSelectionError';
124+
}
125+
}
126+
111127
function makeWriteConcernResultObject(input) {
112128
const output = Object.assign({}, input);
113129

@@ -246,6 +262,7 @@ module.exports = {
246262
MongoNetworkError,
247263
MongoParseError,
248264
MongoTimeoutError,
265+
MongoServerSelectionError,
249266
MongoWriteConcernError,
250267
mongoErrorContextSymbol,
251268
isRetryableError,

lib/core/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = {
2020
MongoNetworkError: require('./error').MongoNetworkError,
2121
MongoParseError: require('./error').MongoParseError,
2222
MongoTimeoutError: require('./error').MongoTimeoutError,
23+
MongoServerSelectionError: require('./error').MongoServerSelectionError,
2324
MongoWriteConcernError: require('./error').MongoWriteConcernError,
2425
mongoErrorContextSymbol: require('./error').mongoErrorContextSymbol,
2526
// Core

lib/core/sdam/server_selection.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const TopologyType = require('./common').TopologyType;
44
const ReadPreference = require('../topologies/read_preference');
55
const MongoError = require('../error').MongoError;
66
const calculateDurationInMs = require('../utils').calculateDurationInMs;
7-
const MongoTimeoutError = require('../error').MongoTimeoutError;
7+
const MongoServerSelectionError = require('../error').MongoServerSelectionError;
88

99
const common = require('./common');
1010
const STATE_CLOSED = common.STATE_CLOSED;
@@ -260,7 +260,7 @@ function selectServers(topology, selector, timeout, start, callback) {
260260
const duration = calculateDurationInMs(start);
261261
if (duration >= timeout) {
262262
return callback(
263-
new MongoTimeoutError(`Server selection timed out after ${timeout} ms`),
263+
new MongoServerSelectionError(`Server selection timed out after ${timeout} ms`),
264264
topology.description
265265
);
266266
}
@@ -306,7 +306,7 @@ function selectServers(topology, selector, timeout, start, callback) {
306306
const iterationTimer = setTimeout(() => {
307307
topology.removeListener('topologyDescriptionChanged', descriptionChangedHandler);
308308
callback(
309-
new MongoTimeoutError(
309+
new MongoServerSelectionError(
310310
`Server selection timed out after ${timeout} ms`,
311311
topology.description
312312
)

test/unit/sdam/server_selection/spec.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const path = require('path');
33
const fs = require('fs');
44
const core = require('../../../../lib/core');
55
const Topology = core.Topology;
6-
const MongoTimeoutError = core.MongoTimeoutError;
6+
const MongoServerSelectionError = core.MongoServerSelectionError;
77
const ReadPreference = core.ReadPreference;
88

99
// TODO: these should be from `core` when legacy topologies are removed
@@ -275,7 +275,7 @@ function executeServerSelectionTest(testDefinition, options, testDone) {
275275
}
276276

277277
// default to serverSelectionTimeoutMS of `100` for unit tests
278-
topology.selectServer(selector, { serverSelectionTimeoutMS: 100 }, (err, server) => {
278+
topology.selectServer(selector, { serverSelectionTimeoutMS: 50 }, (err, server) => {
279279
// are we expecting an error?
280280
if (testDefinition.error) {
281281
if (!err) {
@@ -287,7 +287,7 @@ function executeServerSelectionTest(testDefinition, options, testDone) {
287287

288288
if (err) {
289289
// this is another expected error case
290-
if (expectedServers.length === 0 && err instanceof MongoTimeoutError) return done();
290+
if (expectedServers.length === 0 && err instanceof MongoServerSelectionError) return done();
291291
return done(err);
292292
}
293293

0 commit comments

Comments
 (0)