Skip to content

Commit c6779d3

Browse files
authored
Emit errors only when listeners are present (#66)
* Emit errors only when listeners are present * On socket end don't emit an additional event
1 parent 68b275e commit c6779d3

File tree

3 files changed

+7
-11
lines changed

3 files changed

+7
-11
lines changed

lib/protocol/Connection.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,10 @@ Connection.prototype._addListeners = function _addListeners(socket) {
223223
if (cb) {
224224
self._state.receive = null; // a callback should be called only once
225225
cb(err);
226-
} else {
226+
} else if (self.listeners('error').length) {
227227
self.emit('error', err);
228+
} else {
229+
debug('onerror', err);
228230
}
229231
}
230232
socket.on('error', onerror);
@@ -239,7 +241,7 @@ Connection.prototype._addListeners = function _addListeners(socket) {
239241
function onend() {
240242
var err = new Error('Connection closed by server');
241243
err.code = 'EHDBCLOSE';
242-
socket.emit('error', err);
244+
onerror(err);
243245
}
244246
socket.on('end', onend);
245247
};

lib/protocol/ConnectionManager.js

-6
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@ ConnectionManager.prototype._openConnectionMultiDbCase = function _openConnectio
7878
}
7979
}
8080

81-
function onError(err) {
82-
cb(err);
83-
}
84-
conn.on('error', onError);
85-
8681
function handleError(err) {
8782
conn.close();
8883
cb(err);
@@ -99,7 +94,6 @@ ConnectionManager.prototype._openConnectionMultiDbCase = function _openConnectio
9994
}
10095

10196
if (info.isConnected) {
102-
conn.removeListener('error', onError);
10397
cb(null);
10498
} else {
10599
conn._closeSilently();

test/hdb.Client.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -513,12 +513,12 @@ describe('hdb', function () {
513513
databaseName: 'DB0'
514514
});
515515

516-
client._connection.fetchDbConnectInfo = function () {
517-
client._connection.emit('error', new Error('Network error emitted'));
516+
client._connection.fetchDbConnectInfo = function (options, cb) {
517+
cb(new Error('Network error'));
518518
};
519519

520520
client.connect(function (err) {
521-
err.message.should.equal('Could not connect to any host: [ localhost:30013 - Network error emitted ]');
521+
err.message.should.equal('Could not connect to any host: [ localhost:30013 - Network error ]');
522522
done();
523523
});
524524
});

0 commit comments

Comments
 (0)