Skip to content

Commit 842fd2c

Browse files
authored
Merge pull request #356 from sushantdhiman/connection-test
Catching errors in once crashes process
2 parents aff0b1c + 7c8ebc7 commit 842fd2c

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

lib/commands/client_handshake.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ ClientHandshake.prototype.calculateNativePasswordAuthToken = function (authPlugi
6868
ClientHandshake.prototype.handshakeInit = function (helloPacket, connection) {
6969
var command = this;
7070

71-
this.on('error', function (err) {
72-
connection._protocolError = err;
73-
connection.emit('error', err);
71+
this.on('error', function (e) {
72+
connection._protocolError = e;
7473
});
74+
7575
this.handshake = Packets.Handshake.fromPacket(helloPacket);
7676
if (connection.config.debug) {
7777
console.log('Server hello packet: capability flags:%d=(%s)', this.handshake.capabilityFlags,

lib/connection.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,17 @@ function Connection (opts)
103103
return;
104104
}
105105

106-
// TODO: move to protocolError()
107106
if (!connection._protocolError) { // no particular error message before disconnect
108-
connection._protocolError = 'PROTOCOL_CONNECTION_LOST';
107+
connection._protocolError = new Error('Connection lost: The server closed the connection.');
108+
connection._protocolError.fatal = true;
109+
connection._protocolError.code = 'PROTOCOL_CONNECTION_LOST';
109110
}
110-
var err = new Error('Connection lost: The server closed the connection.');
111-
err.fatal = true;
112-
err.code = connection._protocolError;
113-
connection._notifyError(err);
111+
112+
connection._notifyError(connection._protocolError);
114113
});
115114
var handshakeCommand;
116115
if (!this.config.isServer) {
117116
handshakeCommand = new Commands.ClientHandshake(this.config.clientFlags);
118-
handshakeCommand.on('error', function (e) { connection.emit('error', e); });
119117
handshakeCommand.on('end', function () {
120118
connection._handshakePacket = handshakeCommand.handshake;
121119
connection.threadId = handshakeCommand.handshake.connectionId;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var common = require('../../common');
2+
var assert = require('assert');
3+
4+
var callCount = 0;
5+
var exceptionCount = 0;
6+
7+
process.on('uncaughtException', function (err) {
8+
assert.ifError(err);
9+
exceptionCount++;
10+
});
11+
12+
var connection1 = common.createConnection({
13+
password: 'lol'
14+
});
15+
16+
// error will NOT bubble up to process level if `on` is used
17+
connection1.on('error', function () {
18+
callCount++;
19+
});
20+
21+
var connection2 = common.createConnection({
22+
password: 'lol'
23+
});
24+
25+
// error will bubble up to process level if `once` is used
26+
connection2.once('error', function () {
27+
callCount++;
28+
});
29+
30+
process.on('exit', function () {
31+
assert.equal(callCount, 2);
32+
assert.equal(exceptionCount, 0);
33+
});

0 commit comments

Comments
 (0)