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

Commit

Permalink
fix(connect): ensure connection errors are MongoNetworkErrors
Browse files Browse the repository at this point in the history
NODE-1960
  • Loading branch information
mbroadst committed May 8, 2019
1 parent bcb87ca commit 380a386
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/connection/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Connection = require('./connection');
const Query = require('./commands').Query;
const createClientInfo = require('../topologies/shared').createClientInfo;
const MongoError = require('../error').MongoError;
const MongoNetworkError = require('../error').MongoNetworkError;
const defaultAuthProviders = require('../auth/defaultAuthProviders').defaultAuthProviders;
const WIRE_CONSTANTS = require('../wireprotocol/constants');
const MAX_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_WIRE_VERSION;
Expand Down Expand Up @@ -283,7 +284,7 @@ function makeConnection(family, options, _callback) {
if (err == null || err === false) err = true;
errorEvents.forEach(event => socket.removeAllListeners(event));
socket.removeListener('connect', connectHandler);
callback(err, eventName);
callback(new MongoNetworkError(err.message), eventName);
};
}

Expand Down
8 changes: 8 additions & 0 deletions test/tests/unit/connect_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const expect = require('chai').expect;
const connect = require('../../../lib/connection/connect');
const MongoCredentials = require('../../../lib/auth/mongo_credentials').MongoCredentials;
const genClusterTime = require('./common').genClusterTime;
const MongoNetworkError = require('../../../lib/error').MongoNetworkError;

describe('Connect Tests', function() {
const test = {};
Expand Down Expand Up @@ -90,4 +91,11 @@ describe('Connect Tests', function() {
done(err);
});
});

it('should emit `MongoNetworkError` for network errors', function(done) {
connect({ host: 'non-existent', port: 27018 }, err => {
expect(err).to.be.instanceOf(MongoNetworkError);
done();
});
});
});

0 comments on commit 380a386

Please sign in to comment.