Skip to content

Commit 22a9fce

Browse files
committed
Good error handling and test running
1 parent 0aa798d commit 22a9fce

File tree

6 files changed

+69
-11
lines changed

6 files changed

+69
-11
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test:
2+
node test/run.js
3+
4+
.PHONY: test

lib/Connection.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
var Net = require('net');
2-
var Config = require('./Config');
3-
var Protocol = require('./protocol/Protocol');
1+
var Net = require('net');
2+
var Config = require('./Config');
3+
var Protocol = require('./protocol/Protocol');
4+
var EventEmitter = require('events').EventEmitter;
5+
var Util = require('util');
46

57
module.exports = Connection;
8+
Util.inherits(Connection, EventEmitter);
69
function Connection(options) {
10+
EventEmitter.call(this);
11+
712
this.config = new Config(options.config);
813

914
this._socket = options.socket;
@@ -21,6 +26,7 @@ Connection.prototype.connect = function(cb) {
2126
this._protocol.handshake(this.config, cb);
2227

2328
this._socket.on('error', this._handleNetworkError.bind(this));
29+
this._protocol.on('error', this._handleProtocolError.bind(this));
2430
};
2531

2632
Connection.prototype.query = function(sql, cb) {
@@ -36,3 +42,7 @@ Connection.prototype.end = function() {
3642
Connection.prototype._handleNetworkError = function(err) {
3743
this._protocol.fatalError(err);
3844
};
45+
46+
Connection.prototype._handleProtocolError = function(err) {
47+
this.emit('error', err);
48+
};

lib/protocol/Protocol.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ Protocol.prototype._executeSequence = function(sequence) {
9393
var self = this;
9494

9595
sequence
96-
.on('fatalError', function(err) {
97-
self.fatalError(err);
96+
.on('error', function(err) {
97+
self._handleSequenceError(sequence, err);
9898
})
9999
.on('data', function(buffer) {
100100
self.emit('data', buffer);
@@ -115,14 +115,34 @@ Protocol.prototype._dequeue = function() {
115115
}
116116
};
117117

118+
Protocol.prototype._handleSequenceError = function(sequence, err) {
119+
var bubbleUp = (sequence.hasCallback())
120+
? false
121+
: err.fatal && !this._hasPendingCallbacks();
122+
123+
if (bubbleUp) {
124+
this.emit('error', err);
125+
}
126+
127+
if (err.fatal) {
128+
this.fatalError(err);
129+
}
130+
};
131+
132+
Protocol.prototype._hasPendingCallbacks = function() {
133+
return this._queue.some(function(sequence) {
134+
return sequence.hasCallback();
135+
});
136+
};
137+
118138
Protocol.prototype.fatalError = function(err) {
119139
err.fatal = true;
120140

121141
var pendingSequences = this._queue;
122142
this._queue = [];
123143

124144
pendingSequences.forEach(function(sequence) {
125-
sequence.end(err);
145+
sequence.invokeCallback(err);
126146
});
127147
};
128148

lib/protocol/sequences/Sequence.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Sequence.determinePacket = function(byte) {
2121
}
2222
};
2323

24+
Sequence.prototype.hasCallback = function() {
25+
return Boolean(this._callback);
26+
};
27+
2428
Sequence.packetToError = function(packet) {
2529
var err = new Error(packet.message);
2630
err.code = ErrorConstants[packet.errno] || 'UNKNOWN_CODE_PLEASE_REPORT';
@@ -51,18 +55,21 @@ Sequence.prototype._incrementNextPacketNumber = function() {
5155
this._nextPacketNumber = (this._nextPacketNumber + 1) % 256;
5256
};
5357

58+
Sequence.prototype.invokeCallback = function() {
59+
if (this._callback) {
60+
this._callback.apply(this, arguments);
61+
}
62+
};
63+
5464
Sequence.prototype.end = function(err) {
5565
if (this._callback) {
5666
this._callback.apply(this, arguments);
57-
} else if (err) {
58-
this.emit('error', err);
5967
}
6068

6169
this.emit('end');
6270

63-
if (err.fatal) {
64-
this.emit('fatalError', err);
65-
return;
71+
if (err) {
72+
this.emit('error', err);
6673
}
6774
};
6875

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var common = require('../common');
2+
var connection = common.createConnection({password: common.bogusPassword});
3+
var assert = require('assert');
4+
5+
connection.connect();
6+
connection.query('SELECT 1');
7+
8+
var err;
9+
connection.on('error', function(_err) {
10+
err = _err;
11+
});
12+
13+
process.on('exit', function() {
14+
assert.equal(err.code, 'ER_ACCESS_DENIED_ERROR');
15+
assert.equal(err.fatal, true);
16+
});

test/run.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('urun')(__dirname)

0 commit comments

Comments
 (0)