You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-07 13:22:56 +03:00
Parser should only catch parser errors and bubble the rest to the caller.
Signed-off-by: DTrejo <david.daniel.trejo@gmail.com>
This commit is contained in:
@@ -24,6 +24,12 @@ util.inherits(ReplyParser, events.EventEmitter);
|
||||
|
||||
exports.Parser = ReplyParser;
|
||||
|
||||
function IncompleteReadBuffer(message) {
|
||||
this.name = "IncompleteReadBuffer";
|
||||
this.message = message;
|
||||
}
|
||||
util.inherits(IncompleteReadBuffer, Error);
|
||||
|
||||
// Buffer.toString() is quite slow for small strings
|
||||
function small_toString(buf, start, end) {
|
||||
var tmp = "", i;
|
||||
@@ -48,7 +54,7 @@ ReplyParser.prototype._parseResult = function (type) {
|
||||
|
||||
if (end > this._buffer.length) {
|
||||
this._offset = start;
|
||||
throw new Error("too far");
|
||||
throw new IncompleteReadBuffer("Wait for more data.");
|
||||
}
|
||||
|
||||
if (this.options.return_buffers) {
|
||||
@@ -70,7 +76,7 @@ ReplyParser.prototype._parseResult = function (type) {
|
||||
|
||||
if (end > this._buffer.length) {
|
||||
this._offset = start;
|
||||
throw new Error("too far");
|
||||
throw new IncompleteReadBuffer("Wait for more data.");
|
||||
}
|
||||
|
||||
if (this.options.return_buffers) {
|
||||
@@ -99,7 +105,7 @@ ReplyParser.prototype._parseResult = function (type) {
|
||||
|
||||
if (end > this._buffer.length) {
|
||||
this._offset = offset;
|
||||
throw new Error("too far");
|
||||
throw new IncompleteReadBuffer("Wait for more data.");
|
||||
}
|
||||
|
||||
if (this.options.return_buffers) {
|
||||
@@ -129,7 +135,7 @@ ReplyParser.prototype._parseResult = function (type) {
|
||||
ntype = this._buffer[this._offset++];
|
||||
|
||||
if (this._offset > this._buffer.length) {
|
||||
throw new Error("too far");
|
||||
throw new IncompleteReadBuffer("Wait for more data.");
|
||||
}
|
||||
res = this._parseResult(ntype);
|
||||
if (res === undefined) {
|
||||
@@ -202,16 +208,14 @@ ReplyParser.prototype.execute = function (buffer) {
|
||||
|
||||
ret = this._parseResult(type);
|
||||
|
||||
if (ret === -1) {
|
||||
this._offset = offset;
|
||||
break;
|
||||
}
|
||||
|
||||
this.send_reply(ret);
|
||||
}
|
||||
} catch (err) {
|
||||
// catch the error (not enough data), rewind, and wait
|
||||
// for the next packet to appear
|
||||
if (! (err instanceof IncompleteReadBuffer)) {
|
||||
throw err;
|
||||
}
|
||||
this._offset = offset;
|
||||
break;
|
||||
}
|
||||
@@ -272,7 +276,7 @@ ReplyParser.prototype._packetEndOffset = function () {
|
||||
offset++;
|
||||
|
||||
if (offset >= this._buffer.length) {
|
||||
throw new Error("didn't see LF after NL reading multi bulk count (" + offset + " => " + this._buffer.length + ", " + this._offset + ")");
|
||||
throw new IncompleteReadBuffer("didn't see LF after NL reading multi bulk count (" + offset + " => " + this._buffer.length + ", " + this._offset + ")");
|
||||
}
|
||||
}
|
||||
|
||||
|
28
test.js
28
test.js
@@ -291,6 +291,34 @@ tests.MULTI_6 = function () {
|
||||
});
|
||||
};
|
||||
|
||||
tests.FWD_ERRORS_1 = function () {
|
||||
var name = "FWD_ERRORS_1";
|
||||
|
||||
var toThrow = new Error("Forced exception");
|
||||
var recordedError = null;
|
||||
|
||||
var originalHandler = client3.listeners("error").pop();
|
||||
client3.once("error", function (err) {
|
||||
recordedError = err;
|
||||
});
|
||||
|
||||
client3.on("message", function (channel, data) {
|
||||
console.log("incoming");
|
||||
if (channel == name) {
|
||||
assert.equal(data, "Some message");
|
||||
throw toThrow;
|
||||
}
|
||||
});
|
||||
client3.subscribe(name);
|
||||
|
||||
client.publish(name, "Some message");
|
||||
setTimeout(function () {
|
||||
client3.listeners("error").push(originalHandler);
|
||||
assert.equal(recordedError, toThrow, "Should have caught our forced exception");
|
||||
next(name);
|
||||
}, 150);
|
||||
};
|
||||
|
||||
tests.EVAL_1 = function () {
|
||||
var name = "EVAL_1";
|
||||
|
||||
|
Reference in New Issue
Block a user