Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error code into channel error object. #150

Merged
merged 1 commit into from
May 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ C.closeBecause = function(reason, code, k) {
C.closeWithError = function(reason, code, error) {
var self = this;
this.closeBecause(reason, code, function() {
error.code = code;
self.emit('error', error);
});
};
Expand Down Expand Up @@ -400,7 +401,11 @@ C.accept = function(f) {
}
var emsg = "Channel closed by server: " + closeMsg(f);
this.sendImmediately(defs.ChannelCloseOk, {});
this.emit('error', new Error(emsg));

var error = new Error(emsg);
error.code = f.fields.replyCode;
this.emit('error', error);

var s = stackCapture(emsg);
this.toClosed(s);
return;
Expand Down
30 changes: 24 additions & 6 deletions test/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ test("open, close", channelTest(

test("server close", channelTest(
function(ch, done) {
ch.on('error', succeed(done));
ch.on('error', function(error) {
assert.strictEqual(504, error.code);
succeed(done)();
});
open(ch);
},
function(send, await, done, ch) {
Expand Down Expand Up @@ -226,7 +229,10 @@ test("Bad RPC", channelTest(
// We want to see the RPC rejected and the channel closed (with an
// error)
var errLatch = latch(2, done);
ch.on('error', succeed(errLatch));
ch.on('error', function(error) {
assert.strictEqual(505, error.code);
succeed(errLatch)();
});

open(ch)
.then(function() {
Expand All @@ -252,7 +258,10 @@ test("RPC on closed channel", channelTest(
function(ch, done) {
open(ch);
var close = defer(), fail1 = defer(), fail2 = defer();
ch.on('error', close.resolve);
ch.on('error', function(error) {
assert.strictEqual(504, error.code);
close.resolve();
});

function failureCb(d) {
return function(err) {
Expand Down Expand Up @@ -398,7 +407,10 @@ test("zero byte msg", channelTest(
test("bad delivery", channelTest(
function(ch, done) {
var errorAndClose = latch(2, done);
ch.on('error', succeed(errorAndClose));
ch.on('error', function(error) {
assert.strictEqual(505, error.code);
succeed(errorAndClose)();
});
ch.on('close', succeed(errorAndClose));
open(ch);
},
Expand Down Expand Up @@ -449,7 +461,10 @@ test("bad consumer", channelTest(
ch.on('delivery', function() {
throw new Error("I am a bad consumer");
});
ch.on('error', succeed(errorAndClose));
ch.on('error', function(error) {
assert.strictEqual(541, error.code);
succeed(errorAndClose)();
});
ch.on('close', succeed(errorAndClose));
open(ch);
},
Expand All @@ -465,7 +480,10 @@ test("bad send in consumer", channelTest(
function(ch, done) {
var errorAndClose = latch(2, done);
ch.on('close', succeed(errorAndClose));
ch.on('error', succeed(errorAndClose));
ch.on('error', function(error) {
assert.strictEqual(541, error.code);
succeed(errorAndClose)();
});

ch.on('delivery', function() {
ch.sendMessage({routingKey: 'foo',
Expand Down