Skip to content

Commit e271557

Browse files
committed
don't crash if no serverConfig and use default encoding
1 parent 1365e95 commit e271557

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

lib/connection.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function Connection(opts) {
8787

8888
this.clientEncoding = CharsetToEncoding[this.config.charsetNumber];
8989

90-
this.stream.once('error', connection._handleNetworkError.bind(this));
90+
this.stream.on('error', connection._handleNetworkError.bind(this));
9191

9292
// see https://gist.github.com/khoomeister/4985691#use-that-instead-of-bind
9393
this.packetParser = new PacketParser(function(p) {
@@ -123,11 +123,17 @@ function Connection(opts) {
123123
if (!this.config.isServer) {
124124
handshakeCommand = new Commands.ClientHandshake(this.config.clientFlags);
125125
handshakeCommand.on('end', function() {
126+
// this happens when handshake finishes early and first packet is error
127+
// and not server hello ( for example, 'Too many connactions' error)
128+
if (!handshakeCommand.handshake) {
129+
return;
130+
}
126131
connection._handshakePacket = handshakeCommand.handshake;
127132
connection.threadId = handshakeCommand.handshake.connectionId;
128133
connection.emit('connect', handshakeCommand.handshake);
129134
});
130135
handshakeCommand.on('error', function(err) {
136+
connection._closing = true;
131137
connection._notifyError(err);
132138
});
133139
this.addCommand(handshakeCommand);
@@ -174,6 +180,7 @@ Connection.prototype._handleFatalError = function(err) {
174180
};
175181

176182
Connection.prototype._handleNetworkError = function(err) {
183+
console.log('_handleNetworkError AAAAA NETWORK ERROR');
177184
this._handleFatalError(err);
178185
};
179186

@@ -212,7 +219,16 @@ Connection.prototype._notifyError = function(err) {
212219
connection._command.onResult(err);
213220
connection._command = null;
214221
} else {
215-
bubbleErrorToConnection = true;
222+
// connection handshake is special because we allow it to be implicit
223+
// if error happened during handshake, but there are others commands in queue
224+
// then bubble error to other commands and not to connection
225+
if (
226+
!(connection._command &&
227+
connection._command.constructor == Commands.ClientHandshake &&
228+
connection._commands.length > 0)
229+
) {
230+
bubbleErrorToConnection = true;
231+
}
216232
}
217233
while ((command = connection._commands.shift())) {
218234
if (command.onResult) {
@@ -900,7 +916,7 @@ Connection.prototype.writeOk = function(args) {
900916
Connection.prototype.writeError = function(args) {
901917
// if we want to send error before initial hello was sent, use default encoding
902918
var encoding = this.serverConfig ? this.serverConfig.encoding : 'cesu8';
903-
this.writePacket(Packets.Error.toPacket(args, this.serverConfig.encoding));
919+
this.writePacket(Packets.Error.toPacket(args, encoding));
904920
};
905921

906922
Connection.prototype.serverHandshake = function serverHandshake(args) {

0 commit comments

Comments
 (0)