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

Commit 6dff326

Browse files
Sophie Saskinmbroadst
Sophie Saskin
authored andcommitted
error handling working, still need to figure out close
1 parent b0fb2e3 commit 6dff326

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

lib/connection/connection.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -599,13 +599,14 @@ function makeConnection(conn, options, callback) {
599599
// Add handlers for events
600600
connection.once('error', err => callback(err, null));
601601
connection.once('timeout', err => callback(err, null));
602-
connection.once('close', err => callback(err, null));
602+
// connection.once('close', err => callback(err, null));
603603
return;
604604
}
605605

606606
function fastFallbackConnect(conn, _options, callback) {
607607
const options = prepareConnectionOptions(conn, _options);
608608

609+
let errors = [];
609610
let connection;
610611
const connectionHandler = (err, _connection) => {
611612
if (_connection) {
@@ -618,16 +619,20 @@ function fastFallbackConnect(conn, _options, callback) {
618619
connection = _connection;
619620
return callback(null, connection);
620621
} else if (err) {
621-
if (conn.logger.isDebug()) {
622-
conn.logger.debug(
623-
`connection ${conn.id} for [${conn.host}:${conn.port}] errored out with [${JSON.stringify(
624-
err
625-
)}]`
626-
);
622+
if (errors.length > 0) {
623+
// an error occurred for the second time, we have officially failed
624+
625+
//empty errors array
626+
errors = [];
627+
628+
// return mongo error to be emitted
629+
return callback(new MongoNetworkError('failed to connect'), null);
627630
}
631+
632+
// otherwise push the error, and wait for subsequent connects
633+
errors.push(err);
628634
return;
629635
}
630-
return;
631636
};
632637

633638
makeConnection(conn, Object.assign({}, { family: 6 }, options), connectionHandler);
@@ -655,6 +660,7 @@ Connection.prototype.connect = function(_options) {
655660

656661
return fastFallbackConnect(this, _options, (err, connection) => {
657662
if (err) {
663+
this.emit('error', err, this);
658664
return;
659665
}
660666
// Add handlers for events

0 commit comments

Comments
 (0)