Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit 380a386

Browse files
committed
fix(connect): ensure connection errors are MongoNetworkErrors
NODE-1960
1 parent bcb87ca commit 380a386

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/connection/connect.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const Connection = require('./connection');
55
const Query = require('./commands').Query;
66
const createClientInfo = require('../topologies/shared').createClientInfo;
77
const MongoError = require('../error').MongoError;
8+
const MongoNetworkError = require('../error').MongoNetworkError;
89
const defaultAuthProviders = require('../auth/defaultAuthProviders').defaultAuthProviders;
910
const WIRE_CONSTANTS = require('../wireprotocol/constants');
1011
const MAX_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_WIRE_VERSION;
@@ -283,7 +284,7 @@ function makeConnection(family, options, _callback) {
283284
if (err == null || err === false) err = true;
284285
errorEvents.forEach(event => socket.removeAllListeners(event));
285286
socket.removeListener('connect', connectHandler);
286-
callback(err, eventName);
287+
callback(new MongoNetworkError(err.message), eventName);
287288
};
288289
}
289290

test/tests/unit/connect_tests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const expect = require('chai').expect;
77
const connect = require('../../../lib/connection/connect');
88
const MongoCredentials = require('../../../lib/auth/mongo_credentials').MongoCredentials;
99
const genClusterTime = require('./common').genClusterTime;
10+
const MongoNetworkError = require('../../../lib/error').MongoNetworkError;
1011

1112
describe('Connect Tests', function() {
1213
const test = {};
@@ -90,4 +91,11 @@ describe('Connect Tests', function() {
9091
done(err);
9192
});
9293
});
94+
95+
it('should emit `MongoNetworkError` for network errors', function(done) {
96+
connect({ host: 'non-existent', port: 27018 }, err => {
97+
expect(err).to.be.instanceOf(MongoNetworkError);
98+
done();
99+
});
100+
});
93101
});

0 commit comments

Comments
 (0)