From 5321a1746f708fd6a7f3c2903f65d94fa57877fd Mon Sep 17 00:00:00 2001 From: Orion Henry Date: Wed, 29 Sep 2010 17:10:01 -0700 Subject: [PATCH] have the parser try to recover from errors when it has them and communicate this back to the user --- index.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index c8210e5875..444f57fb87 100644 --- a/index.js +++ b/index.js @@ -9,10 +9,7 @@ var net = require("net"), exports.debug_mode = false; function RedisReplyParser() { - this.state = "type"; - this.return_buffer = new Buffer(16384); // for holding replies, might grow - this.tmp_buffer = new Buffer(128); // for holding size fields - + this.reset(); events.EventEmitter.call(this); } sys.inherits(RedisReplyParser, events.EventEmitter); @@ -38,6 +35,12 @@ function to_array(args) { return arr; } +RedisReplyParser.prototype.reset = function() { + this.state = "type"; + this.return_buffer = new Buffer(16384); // for holding replies, might grow + this.tmp_buffer = new Buffer(128); // for holding size fields +} + RedisReplyParser.prototype.execute = function (incoming_buf) { var pos = 0, bd_tmp, bd_str, i; //, state_times = {}, start_execute = new Date(), start_switch, end_switch, old_state; @@ -348,7 +351,7 @@ function RedisClient(stream) { self.return_reply(reply); }); self.reply_parser.on("error", function (err) { - console.log("Redis reply parser error: " + err.stack); + self.emit("error", new Error("Redis reply parser error: " + err.stack)); }); self.retry_timer = null; @@ -454,7 +457,8 @@ RedisClient.prototype.on_data = function (data) { try { this.reply_parser.execute(data); } catch (err) { - console.log("Exception in RedisReplyParser: " + err.stack); + this.reply_parser.reset(); // the parser has state - must reset it or it can never recover + this.emit("error", err); // pass the error along } };