Skip to content

Commit c6ae783

Browse files
author
Ruben Bridgewater
committed
Remove try callbacks and emit an error in case of no callback has been provided
1 parent 1b261fc commit c6ae783

File tree

2 files changed

+15
-44
lines changed

2 files changed

+15
-44
lines changed

index.js

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -152,27 +152,15 @@ RedisClient.prototype.flush_and_error = function (message) {
152152
while (this.offline_queue.length > 0) {
153153
command_obj = this.offline_queue.shift();
154154
if (typeof command_obj.callback === "function") {
155-
try {
156-
command_obj.callback(error);
157-
} catch (callback_err) {
158-
process.nextTick(function () {
159-
throw callback_err;
160-
});
161-
}
155+
command_obj.callback(error);
162156
}
163157
}
164158
this.offline_queue = new Queue();
165159

166160
while (this.command_queue.length > 0) {
167161
command_obj = this.command_queue.shift();
168162
if (typeof command_obj.callback === "function") {
169-
try {
170-
command_obj.callback(error);
171-
} catch (callback_err) {
172-
process.nextTick(function () {
173-
throw callback_err;
174-
});
175-
}
163+
command_obj.callback(error);
176164
}
177165
}
178166
this.command_queue = new Queue();
@@ -529,38 +517,13 @@ RedisClient.prototype.return_error = function (err) {
529517
this.emit("drain");
530518
this.should_buffer = false;
531519
}
532-
533-
try {
520+
if (command_obj.callback) {
534521
command_obj.callback(err);
535-
} catch (callback_err) {
536-
// if a callback throws an exception, re-throw it on a new stack so the parser can keep going
537-
process.nextTick(function () {
538-
throw callback_err;
539-
});
522+
} else {
523+
this.emit('error', err);
540524
}
541525
};
542526

543-
// if a callback throws an exception, re-throw it on a new stack so the parser can keep going.
544-
// if a domain is active, emit the error on the domain, which will serve the same function.
545-
// put this try/catch in its own function because V8 doesn't optimize this well yet.
546-
function try_callback(callback, reply) {
547-
try {
548-
callback(null, reply);
549-
} catch (err) {
550-
if (process.domain) {
551-
var currDomain = process.domain;
552-
currDomain.emit('error', err);
553-
if (process.domain === currDomain) {
554-
currDomain.exit();
555-
}
556-
} else {
557-
process.nextTick(function () {
558-
throw err;
559-
});
560-
}
561-
}
562-
}
563-
564527
// hgetall converts its replies to an Object. If the reply is empty, null is returned.
565528
function reply_to_object(reply) {
566529
var obj = {}, j, jl, key, val;
@@ -638,7 +601,7 @@ RedisClient.prototype.return_reply = function (reply) {
638601
reply = reply_to_object(reply);
639602
}
640603

641-
try_callback(command_obj.callback, reply);
604+
command_obj.callback(null, reply);
642605
} else {
643606
debug("no callback for reply: " + (reply && reply.toString && reply.toString()));
644607
}
@@ -662,7 +625,7 @@ RedisClient.prototype.return_reply = function (reply) {
662625
// reply[1] can be null
663626
var reply1String = (reply[1] === null) ? null : reply[1].toString();
664627
if (command_obj && typeof command_obj.callback === "function") {
665-
try_callback(command_obj.callback, reply1String);
628+
command_obj.callback(null, reply1String);
666629
}
667630
this.emit(type, reply1String, reply[2]); // channel, count
668631
} else {

test/commands/eval.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ describe("The 'eval' method", function () {
118118
helper.serverVersionAtLeast.call(this, client, [2, 5, 0]);
119119
client.evalsha('ffffffffffffffffffffffffffffffffffffffff', 0, helper.isError(done));
120120
});
121+
122+
it('emits an error if SHA does not exist and no callback has been provided', function (done) {
123+
client.on('error', function (err) {
124+
assert.equal(err.message, 'NOSCRIPT No matching script. Please use EVAL.');
125+
done();
126+
});
127+
client.evalsha('ffffffffffffffffffffffffffffffffffffffff', 0);
128+
});
121129
});
122130

123131
it('allows a key to be incremented, and performs appropriate conversion from LUA type', function (done) {

0 commit comments

Comments
 (0)