diff --git a/lib/queue.js b/lib/queue.js index 341a668b..91d15144 100644 --- a/lib/queue.js +++ b/lib/queue.js @@ -266,15 +266,17 @@ Queue.prototype.unbind = function (exchange, routingKey) { var exchangeName = exchange instanceof Exchange ? exchange.name : exchange; - // Decrement binding count. - this._bindings[exchangeName][routingKey]--; - if (!this._bindings[exchangeName][routingKey]) { - delete this._bindings[exchangeName][routingKey]; - } + if(this._bindings[exchangeName]) { + // Decrement binding count. + this._bindings[exchangeName][routingKey]--; + if (!this._bindings[exchangeName][routingKey]) { + delete this._bindings[exchangeName][routingKey]; + } - // If there are no more bindings to this exchange, delete the key for the exchange. - if (!_.keys(this._bindings[exchangeName]).length){ - delete this._bindings[exchangeName]; + // If there are no more bindings to this exchange, delete the key for the exchange. + if (!_.keys(this._bindings[exchangeName]).length){ + delete this._bindings[exchangeName]; + } } return this._taskPush(methods.queueUnbindOk, function () { diff --git a/test/test-unbind-unknown-exchange.js b/test/test-unbind-unknown-exchange.js new file mode 100644 index 00000000..c5bf36af --- /dev/null +++ b/test/test-unbind-unknown-exchange.js @@ -0,0 +1,27 @@ +var conn = require('./harness').createConnection(); + +var caughtError = false; + +var later = function(fun) { + setTimeout(fun, 500); +}; +conn.once('ready', function () { + puts("connected to " + conn.serverProperties.product); + + var q = conn.queue('node-simple-queue'); + + try { + q.unbind('unknown-exchange', 'routing-key'); + } catch(e) { + caughtError = true; + } + + later(function() { + conn.end(); + process.exit(); + }); +}); + +process.addListener('exit', function () { + assert.equal(caughtError, false); +});