1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

Remove try callbacks and emit an error in case of no callback has been provided

This commit is contained in:
Ruben Bridgewater
2015-09-03 21:57:13 +02:00
parent 1b261fcaad
commit c6ae7832a3
2 changed files with 15 additions and 44 deletions

View File

@@ -152,13 +152,7 @@ RedisClient.prototype.flush_and_error = function (message) {
while (this.offline_queue.length > 0) { while (this.offline_queue.length > 0) {
command_obj = this.offline_queue.shift(); command_obj = this.offline_queue.shift();
if (typeof command_obj.callback === "function") { if (typeof command_obj.callback === "function") {
try { command_obj.callback(error);
command_obj.callback(error);
} catch (callback_err) {
process.nextTick(function () {
throw callback_err;
});
}
} }
} }
this.offline_queue = new Queue(); this.offline_queue = new Queue();
@@ -166,13 +160,7 @@ RedisClient.prototype.flush_and_error = function (message) {
while (this.command_queue.length > 0) { while (this.command_queue.length > 0) {
command_obj = this.command_queue.shift(); command_obj = this.command_queue.shift();
if (typeof command_obj.callback === "function") { if (typeof command_obj.callback === "function") {
try { command_obj.callback(error);
command_obj.callback(error);
} catch (callback_err) {
process.nextTick(function () {
throw callback_err;
});
}
} }
} }
this.command_queue = new Queue(); this.command_queue = new Queue();
@@ -529,38 +517,13 @@ RedisClient.prototype.return_error = function (err) {
this.emit("drain"); this.emit("drain");
this.should_buffer = false; this.should_buffer = false;
} }
if (command_obj.callback) {
try {
command_obj.callback(err); command_obj.callback(err);
} catch (callback_err) { } else {
// if a callback throws an exception, re-throw it on a new stack so the parser can keep going this.emit('error', err);
process.nextTick(function () {
throw callback_err;
});
} }
}; };
// if a callback throws an exception, re-throw it on a new stack so the parser can keep going.
// if a domain is active, emit the error on the domain, which will serve the same function.
// put this try/catch in its own function because V8 doesn't optimize this well yet.
function try_callback(callback, reply) {
try {
callback(null, reply);
} catch (err) {
if (process.domain) {
var currDomain = process.domain;
currDomain.emit('error', err);
if (process.domain === currDomain) {
currDomain.exit();
}
} else {
process.nextTick(function () {
throw err;
});
}
}
}
// hgetall converts its replies to an Object. If the reply is empty, null is returned. // hgetall converts its replies to an Object. If the reply is empty, null is returned.
function reply_to_object(reply) { function reply_to_object(reply) {
var obj = {}, j, jl, key, val; var obj = {}, j, jl, key, val;
@@ -638,7 +601,7 @@ RedisClient.prototype.return_reply = function (reply) {
reply = reply_to_object(reply); reply = reply_to_object(reply);
} }
try_callback(command_obj.callback, reply); command_obj.callback(null, reply);
} else { } else {
debug("no callback for reply: " + (reply && reply.toString && reply.toString())); debug("no callback for reply: " + (reply && reply.toString && reply.toString()));
} }
@@ -662,7 +625,7 @@ RedisClient.prototype.return_reply = function (reply) {
// reply[1] can be null // reply[1] can be null
var reply1String = (reply[1] === null) ? null : reply[1].toString(); var reply1String = (reply[1] === null) ? null : reply[1].toString();
if (command_obj && typeof command_obj.callback === "function") { if (command_obj && typeof command_obj.callback === "function") {
try_callback(command_obj.callback, reply1String); command_obj.callback(null, reply1String);
} }
this.emit(type, reply1String, reply[2]); // channel, count this.emit(type, reply1String, reply[2]); // channel, count
} else { } else {

View File

@@ -118,6 +118,14 @@ describe("The 'eval' method", function () {
helper.serverVersionAtLeast.call(this, client, [2, 5, 0]); helper.serverVersionAtLeast.call(this, client, [2, 5, 0]);
client.evalsha('ffffffffffffffffffffffffffffffffffffffff', 0, helper.isError(done)); client.evalsha('ffffffffffffffffffffffffffffffffffffffff', 0, helper.isError(done));
}); });
it('emits an error if SHA does not exist and no callback has been provided', function (done) {
client.on('error', function (err) {
assert.equal(err.message, 'NOSCRIPT No matching script. Please use EVAL.');
done();
});
client.evalsha('ffffffffffffffffffffffffffffffffffffffff', 0);
});
}); });
it('allows a key to be incremented, and performs appropriate conversion from LUA type', function (done) { it('allows a key to be incremented, and performs appropriate conversion from LUA type', function (done) {