Skip to content

Commit 8076fa3

Browse files
committed
Add method and class ids to channel errors
1 parent 120c5f9 commit 8076fa3

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

bin/generate-defs.js

+4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ for (var i = 0, len = defs.classes.length; i < len; i++) {
8282
methods[name] = {
8383
id: methodId(clazz, method),
8484
name: name,
85+
methodId: method.id,
86+
clazzId: clazz.id,
8587
clazz: clazz.name,
8688
args: method['arguments'].map(argument),
8789
isReply: method.answer,
@@ -518,6 +520,8 @@ function decoderFn(method) {
518520

519521
function infoObj(thing) {
520522
var info = JSON.stringify({id: thing.id,
523+
classId: thing.clazzId,
524+
methodId: thing.methodId,
521525
name: thing.name,
522526
args: thing.args});
523527
println('var %s = module.exports.%s = %s;',

lib/channel.js

+19-7
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ C._rpc = function(method, fields, expect, cb) {
103103
// We have detected a problem, so it's up to us to close the
104104
// channel
105105
var expectedName = methodName(expect);
106+
106107
var e = new Error(fmt("Expected %s; got %s",
107108
expectedName, inspect(f, false)));
108-
self.closeWithError(fmt('Expected %s; got %s',
109+
self.closeWithError(f.id, fmt('Expected %s; got %s',
109110
expectedName, methodName(f.id)),
110111
defs.constants.UNEXPECTED_FRAME, e);
111112
return cb(e);
@@ -124,7 +125,11 @@ C._rpc = function(method, fields, expect, cb) {
124125
? fmt("Operation failed: %s; %s",
125126
methodName(method), closeMsg(err))
126127
: fmt("Channel closed by server: %s", closeMsg(err));
127-
return cb(new Error(e));
128+
var closeFrameError = new Error(e);
129+
closeFrameError.code = err.fields.replyCode;
130+
closeFrameError.classId = err.fields.classId;
131+
closeFrameError.methodId = err.fields.methodId;
132+
return cb(closeFrameError);
128133
}
129134
}
130135

@@ -210,10 +215,15 @@ C.closeBecause = function(reason, code, k) {
210215
// If we close because there's been an error, we need to distinguish
211216
// between what we tell the server (`reason`) and what we report as
212217
// the cause in the client (`error`).
213-
C.closeWithError = function(reason, code, error) {
218+
C.closeWithError = function(id, reason, code, error) {
214219
var self = this;
215220
this.closeBecause(reason, code, function() {
216221
error.code = code;
222+
// content frames and consumer errors do not provide a method a class/method ID
223+
if (id) {
224+
error.classId = defs.info(id).classId;
225+
error.methodId = defs.info(id).methodId;
226+
}
217227
self.emit('error', error);
218228
});
219229
};
@@ -232,15 +242,15 @@ C.acceptMessageFrame = function(f) {
232242
}
233243
catch (msg) {
234244
if (typeof msg === 'string') {
235-
this.closeWithError(msg, defs.constants.UNEXPECTED_FRAME,
245+
this.closeWithError(f.id, msg, defs.constants.UNEXPECTED_FRAME,
236246
new Error(msg));
237247
}
238248
else if (msg instanceof Error) {
239-
this.closeWithError('Error while processing message',
249+
this.closeWithError(f.id, 'Error while processing message',
240250
defs.constants.INTERNAL_ERROR, msg);
241251
}
242252
else {
243-
this.closeWithError('Internal error while processing message',
253+
this.closeWithError(f.id, 'Internal error while processing message',
244254
defs.constants.INTERNAL_ERROR,
245255
new Error(msg.toString()));
246256
}
@@ -405,6 +415,8 @@ C.accept = function(f) {
405415

406416
var error = new Error(emsg);
407417
error.code = f.fields.replyCode;
418+
error.classId = f.fields.classId;
419+
error.methodId = f.fields.methodId;
408420
this.emit('error', error);
409421

410422
var s = stackCapture(emsg);
@@ -413,7 +425,7 @@ C.accept = function(f) {
413425

414426
case defs.BasicFlow:
415427
// RabbitMQ doesn't send this, it just blocks the TCP socket
416-
return this.closeWithError("Flow not implemented",
428+
return this.closeWithError(f.id, "Flow not implemented",
417429
defs.constants.NOT_IMPLEMENTED,
418430
new Error('Flow not implemented'));
419431

test/channel.js

+12
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ test("server close", channelTest(
128128
function(ch, done) {
129129
ch.on('error', function(error) {
130130
assert.strictEqual(504, error.code);
131+
assert.strictEqual(0, error.classId);
132+
assert.strictEqual(0, error.methodId);
131133
succeed(done)();
132134
});
133135
open(ch);
@@ -229,6 +231,8 @@ test("Bad RPC", channelTest(
229231
var errLatch = latch(2, done);
230232
ch.on('error', function(error) {
231233
assert.strictEqual(505, error.code);
234+
assert.strictEqual(60, error.classId);
235+
assert.strictEqual(72, error.methodId);
232236
succeed(errLatch)();
233237
});
234238

@@ -259,6 +263,8 @@ test("RPC on closed channel", channelTest(
259263
var close = new Promise(function(resolve) {
260264
ch.on('error', function(error) {
261265
assert.strictEqual(504, error.code);
266+
assert.strictEqual(0, error.classId);
267+
assert.strictEqual(0, error.methodId);
262268
resolve();
263269
});
264270
});
@@ -415,6 +421,8 @@ test("bad delivery", channelTest(
415421
var errorAndClose = latch(2, done);
416422
ch.on('error', function(error) {
417423
assert.strictEqual(505, error.code);
424+
assert.strictEqual(60, error.classId);
425+
assert.strictEqual(60, error.methodId);
418426
succeed(errorAndClose)();
419427
});
420428
ch.on('close', succeed(errorAndClose));
@@ -469,6 +477,8 @@ test("bad consumer", channelTest(
469477
});
470478
ch.on('error', function(error) {
471479
assert.strictEqual(541, error.code);
480+
assert.strictEqual(undefined, error.classId);
481+
assert.strictEqual(undefined, error.methodId);
472482
succeed(errorAndClose)();
473483
});
474484
ch.on('close', succeed(errorAndClose));
@@ -488,6 +498,8 @@ test("bad send in consumer", channelTest(
488498
ch.on('close', succeed(errorAndClose));
489499
ch.on('error', function(error) {
490500
assert.strictEqual(541, error.code);
501+
assert.strictEqual(undefined, error.classId);
502+
assert.strictEqual(undefined, error.methodId);
491503
succeed(errorAndClose)();
492504
});
493505

0 commit comments

Comments
 (0)