Skip to content

Commit

Permalink
fix(connect): fixed syntax issue in connect error handler
Browse files Browse the repository at this point in the history
Connect error handler was attempting to read properties off
of an error that had a chance of being null. Removed that,
and replaced the error generation to be more explicit.

Fixes NODE-1960
Fixes NODE-1968
  • Loading branch information
daprahamian committed Aug 13, 2019
1 parent 41e7c7e commit ff7166d
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/core/connection/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,9 @@ function makeConnection(family, options, _callback) {
const errorEvents = ['error', 'close', 'timeout', 'parseError', 'connect'];
function errorHandler(eventName) {
return err => {
if (err == null || err === false) err = true;
errorEvents.forEach(event => socket.removeAllListeners(event));
socket.removeListener('connect', connectHandler);
callback(new MongoNetworkError(err.message), eventName);
callback(connectionFailureError(eventName, err), eventName);
};
}

Expand Down Expand Up @@ -355,4 +354,17 @@ function authenticate(conn, credentials, callback) {
});
}

function connectionFailureError(type, err) {
switch (type) {
case 'error':
return new MongoNetworkError(err);
case 'timeout':
return new MongoNetworkError(`connection timed out`);
case 'close':
return new MongoNetworkError(`connection closed`);
default:
return new MongoNetworkError(`unknown network error`);
}
}

module.exports = connect;

0 comments on commit ff7166d

Please sign in to comment.